gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:20 28 4
gpt4 key购买 nike

我正在尝试以编程方式执行 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");

执行命令类:

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);
}
}

日志中的问题:

07-31 15:26:31.832 18716-18716/com.example E/Output﹕ cannot bind 'tcp:5038' * daemon not running. starting it now on port 5038 *

我无法截取屏幕截图,但如果我在命令提示符下运行相同的命令,则一切正常。

但是如果我执行

new ExecuteCommand(MainActivity.this).execute("ls");

这很好用。命令哪里出了问题?

最佳答案

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

这将是干净的:

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

关于android - 以编程方式执行 adb 命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743477/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com