问题
I have a rooted Android device. At one point I launch a secondary application from my primary application like so:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.app.package");
startActivityForResult(intent, 100);
I want to be able to kill this secondary application from the primary application. I am trying the following general procedure:
// At an earlier point in time...
Runtime.getRuntime().exec("su");
// The user grants permission
// ...
// At a later point in time...
Runtime.getRuntime().exec("su am force-stop com.app.package");
Unfortunately, this does not kill the app, with no hint as to why from logcat.
If I try to run the killing command as "am force-stop com.app.package" instead of "su am force-stop com.app.package", logcat says that I don't have permission, even though I got superuser permission from running "su" earlier.
回答1:
Found a solution:
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("adb shell" + "\n");
os.flush();
os.writeBytes("am force-stop com.xxxxxx" + "\n");
os.flush();
来源:https://stackoverflow.com/questions/37864816/android-kill-app-from-another-app-on-a-rooted-device