Android - 从另一个应用程序杀死应用程序(在有根设备上)

Android - Kill App from another App (on a rooted device)

我有一个 root 的 Android 设备。有一次,我从主应用程序启动了一个辅助应用程序,如下所示:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.app.package");
startActivityForResult(intent, 100);

我希望能够从主应用程序中终止这个辅助应用程序。我正在尝试以下一般程序:

// 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");

不幸的是,这并没有终止应用程序,logcat没有提示原因。

如果我尝试 运行 杀死命令作为 "am force-stop com.app.package" 而不是 "su am force-stop com.app.package",logcat 说我没有权限,即使我有超级用户早些时候 运行ning "su" 的许可。

找到解决方案:

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();