【发布时间】:2020-09-17 14:48:25
【问题描述】:
我正在尝试从我的 Android 应用程序的命令行启动另一个应用程序(不是我的)的服务。但我注意到它只有在我运行“su”时才有效。我的手机当然是“root”了。 也许还有另一种方法可以在不需要执行 shell 命令的情况下启动应用程序的服务?
此代码有效:
try {
Process process = Runtime.getRuntime().exec("su", null,null);
OutputStream outputStream = process.getOutputStream();
outputStream.write(("am startservice -a com.companyname.notmyapp.TEST --option a 1").getBytes("ASCII"));
outputStream.flush();
outputStream.close();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
这个不是:
try {
Process process = Runtime.getRuntime().exec("am startservice -a com.companyname.notmyapp.TEST --option a 1", null,null);
//OutputStream outputStream = process.getOutputStream();
//outputStream.flush();
//outputStream.close();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
【问题讨论】:
标签: java android command-line process su