Search code examples
androidandroid-intentrootkillsuperuser

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


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.


Solution

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