我正在尝试从我的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();
}
答案 0 :(得分:0)
Intent intent = new Intent(Intent.ACTION_VIEW); 字符串packageName =“ com.ang.chapter_2_service”; //要开始的包名称
String className =“ com.ang.chapter_2.poolBinder.BinderPoolService”; //服务全名你要开始的名字 intent.setClassName(packageName,className); startService(intent); //或bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
活动与此相同。
当然,要启动的服务或活动需要manifest.xml中的标签:
android:exported="true"