Home > Blockchain >  How to run adb command in Android Studio sourcecode? [duplicate]
How to run adb command in Android Studio sourcecode? [duplicate]

Time:09-27

        muteButton.setOnClickListener {
            Runtime.getRuntime().exec("adb shell settings put system csc_pref_camera_forced_shuttersound_key 0");
        }

This is my code, and below is Log message.

    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.io.IOException: Cannot run program "adb": error=13, Permission denied
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
        at java.lang.Runtime.exec(Runtime.java:698)
        at java.lang.Runtime.exec(Runtime.java:528)
        at java.lang.Runtime.exec(Runtime.java:425)
        at pakage_name.MainActivity.onCreate$lambda-0(MainActivity.kt:16)
        at pakage_name.MainActivity.$r8$lambda$ho6D_gaRxTXOpx0ACOxYNLNSY60(Unknown Source:0)

I think

Caused by: java.io.IOException: Cannot run program "adb": error=13, Permission denied

is a cause. How should I modify my code?

CodePudding user response:

The adb command is a program which is installed on your PC and it opens a shell in the android device. If you use native code, you don't need (and cannot) to use it - just remove it and call the command directly:
Runtime.getRuntime().exec("settings put system csc_pref_camera_forced_shuttersound_key 0");

  • Related