java为什么在Android中从命令行启动服务需要根访问权限(su)?
我正在尝试从Android应用程序的命令行启动另一个应用程序(不是我的)的服务。但我注意到,只有当我运行“su”时,它才会起作用。我的手机当然是“根”。 也许有另一种方法可以启动应用程序的服务,而不需要执行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();
}
# 1 楼答案
意向意向=新意向(意向.行动\视图); String packageName=“com.ang.chapter_2_service”//要启动的包名称
String className=“com.ang.chapter_2.poolBinder.BinderPoolService”//要启动的服务全名 意图setClassName(packageName,className); startService(意向)//或bindService(intent、mConnection、Context.BIND\u AUTO\u CREATE)
活动与此相同
当然,要启动的服务或活动需要清单中的标记。xml: