Android - Kill App from another App (on a rooted d

2020-06-06 03:52发布

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条回答
做个烂人
2楼-- · 2020-06-06 04:32

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();
查看更多
登录 后发表回答