在app中运行adb命令会拒绝权限

时间:2016-12-12 16:01:47

标签: android

我正在构建一个试图执行某些命令的应用程序,例如" top"和别的。 我正在研究android 7.0。 我在较旧的Android版本中尝试此代码,但当我在牛轧糖上尝试它时,我得到以下错误权限被拒绝:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.matant.cmdlineapp, PID: 7662
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:325)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                  at java.lang.Thread.run(Thread.java:761)
               Caused by: java.lang.RuntimeException: java.io.IOException: Cannot run program "adb shell top": error=13, Permission denied
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:88)
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36)
                  at android.os.AsyncTask$2.call(AsyncTask.java:305)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                  at java.lang.Thread.run(Thread.java:761) 
               Caused by: java.io.IOException: Cannot run program "adb shell top": error=13, Permission denied
                  at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:62)
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36) 
                  at android.os.AsyncTask$2.call(AsyncTask.java:305) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                  at java.lang.Thread.run(Thread.java:761) 
               Caused by: java.io.IOException: error=13, Permission denied
                  at java.lang.UNIXProcess.forkAndExec(Native Method)
                  at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
                  at java.lang.ProcessImpl.start(ProcessImpl.java:128)
                  at java.lang.ProcessBuilder.start(ProcessBuilder.java:964)
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:62) 
                  at com.example.matant.cmdlineapp.MainActivity$RunningTopCommand.doInBackground(MainActivity.java:36) 
                  at android.os.AsyncTask$2.call(AsyncTask.java:305) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                  at java.lang.Thread.run(Thread.java:761) 

我正在执行的代码是:

protected Void doInBackground(String... params) {
        //Execution en BackGround
        Log.d("BACKGROUND", "START");
        Log.d("BACK PARAM 0", params[0]);
        //Log.d("BACK PARAM 1", params[1]);

        try {
            // Executes the command.
            //Process process = Runtime.getRuntime().exec("adb version");
            ProcessBuilder processBuilder = new ProcessBuilder("adb shell top");
            Process process = processBuilder.start();

            // Reads stdout.
            // NOTE: You can write to stdin of the command using
            //       process.getOutputStream().
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
                Log.d("reader val: ",output.toString());
                publishProgress(output.toString());
            }
            reader.close();

            return null;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

1 个答案:

答案 0 :(得分:2)

来自this

  众所周知,

/ sys和/ proc泄漏了侧信道信息   关于过程,可用于推断状态的信息   过程。例如,多年来已经记录了/ proc   访问可用于监控应用程序启动,启用网络钓鱼   攻击。

因此,随着N的发布,开发团队故意限制/sys/proc的访问权限。如果我没有弄错的话,adb top命令也完全依赖于/proc州的阅读。我认为这是因为不能在没有遇到从Android N开始的权限被拒绝错误的情况下通过top执行ProcessBuilder命令。