英文:
Why starting a service from command line in Android needs root access (su)?
问题
以下是翻译好的部分:
我正在尝试在我的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();
}
英文:
I am trying to start a service of an another app (not mine) from command line in my Android app. But I've noticed that it works only if I run "su". My phone of course is "rooted".
Maybe there is another way to start a service of an app without needing to execute a shell command?
This code works:
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();
}
This one it doesn't:
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
得分: 0
Intent intent = new Intent(Intent.ACTION_VIEW);
String 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"
英文:
Intent intent = new Intent(Intent.ACTION_VIEW);
String packageName = "com.ang.chapter_2_service"; //the package name what you want to start
String className = "com.ang.chapter_2.poolBinder.BinderPoolService"; //service full name what you want to start
intent.setClassName(packageName, className);
startService(intent);//or bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
the activity is same to this.
Of course the service or activity what you want to start need a tag in manifest.xml:
android:exported="true"
专注分享java语言的经验与见解,让所有开发者获益!
评论