Android 以编程方式执行adb命令时出错

Android 以编程方式执行adb命令时出错,android,shell,adb,screenshot,Android,Shell,Adb,Screenshot,我试图以编程方式执行adb命令 这是我的密码: File f = new File(Environment.getExternalStorageDirectory(), "screen" + System.currentTimeMillis() + ".png"); new ExecuteCommand(MainActivity.this).execute("adb shell screencap -p " + Environment.getExternalStorageDir

我试图以编程方式执行adb命令

这是我的密码:

File f = new File(Environment.getExternalStorageDirectory(), "screen" + System.currentTimeMillis() + ".png");

new ExecuteCommand(MainActivity.this).execute("adb shell screencap -p "
        + Environment.getExternalStorageDirectory().getPath() +
        "/" + "screen" + System.currentTimeMillis() + ".png");
ExecuteCommand类:

public class ExecuteCommand extends AsyncTask<String, String, String> {

    Context mContext=null;
    public ExecuteCommand(Context _ctx)
    {
        mContext =_ctx;
    }

    ProgressDialog progressdailog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressdailog = ProgressDialog.show(mContext,
                "Executing", "Please Wait");
    }
    @Override
    protected String doInBackground(String... params) {
        Process p;
        StringBuffer output = new StringBuffer();
        try {
            p = Runtime.getRuntime().exec(params[0]);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
                p.waitFor();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String response = output.toString();
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressdailog.dismiss();
        Log.e("Output", result);
    }
}

这个很好用。命令中的问题在哪里?

adb shell
用于在PC中执行命令以尝试访问设备。但是,当您在设备本身上执行命令时,不需要
adb shell

这将是干净的:

new ExecuteCommand(MainActivity.this).execute("screencap -p " + f);

我猜权限有问题

使用新的API来实现这一点


请参阅源代码。

在本网站和上都有过类似的讨论,但TL:DR是您无法从设备上运行所有shell命令。大多数应用程序都需要仅授予系统应用程序的权限。例如,使用
pm
shell命令需要各种软件包权限,其中大多数仅为系统权限。您可以查看权限列表


您必须为设备设置根目录,或者寻找另一种实现所需功能的方法。

这不起作用。现在输出为[07-31 15:38:11.494 1039:0x502 D/KeyguardViewMediator]setHidden FalseShidden false可能不是问题所在。这是删除adb shell命令时的唯一输出?来自KeyguardViewMediator的日志不会影响此输出。看看别的地方。屏幕记录甚至可以在键盘上记录,所以是否可见并不重要。是的,但屏幕截图不保存或可能不被接受!!!但是我需要使用命令行中的一些东西,因为我还必须使用其他adb命令!!
new ExecuteCommand(MainActivity.this).execute("screencap -p " + f);