以编程方式执行adb命令时出错 [英] Error while executing adb command programmatically
问题描述
我正在尝试以编程方式执行adb命令
I am trying to execute adb commands programmatically
这是我的代码:
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类:
ExecuteCommand Class:
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:无法绑定'tcp:5038'*守护程序未运行。现在在端口5038上启动*
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 *
我无法截屏,但是如果我在命令提示符下运行了相同的命令,这很好。
I am not able to take a screenshot, but if I ran the same command on command prompt then this is working fine.
但是如果我执行
new ExecuteCommand(MainActivity.this).execute("ls");
这很好用。命令中的问题在哪里?
this works fine. Where is the problem in command?
推荐答案
adb shell
在以下情况下使用您正在PC中执行命令以尝试访问设备。但是当您在设备本身上执行命令时,不需要 adb shell
adb shell
is used when you're executing the command in your PC trying to access the device. But when you are executing the command on device itself, you don't need adb shell
这将很干净:
new ExecuteCommand(MainActivity.this).execute("screencap -p " + f);
这篇关于以编程方式执行adb命令时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!