简体   繁体   中英

Kill process on Android by pid

I found the some similar questions around stackoverflow, but I haven't found one that works in my case.

So far, I tried:

    Runtime.getRuntime().exec(new String[]{"su","-c","kill -9 "+pid});
    //am.killBackgroundProcesses(pkgName);
    //android.os.Process.killProcess(pid); 

While if I typed su -c kill -9 pid from adb , it worked! Why not work programmatically?

Do I need to put any permission in AndroidManifest.xml ?

Btw, the device has been rooted.

For one thing, it's probably not a good idea to call su in this way. (every variation of pid will likely need to be authorized by whatever app is managing your calls to su ) Most people call sh and then write the command they want to execute.

The error I noticed immediately is that your -9 needs to be in a different part of the array. For example:

 
 
 
  
  Runtime.getRuntime().exec(new String[]{"su", "-c", "kill", "-9", ""+pid});
 
  

Edit: Actually, it seems like this should work. I forgot that the argument to -c needs to be the entire command.

You can find some example code here for executing a command as root on Android.

Another thing you can do is try specifying the full path to the su and kill binaries. (which may be phone-specific, so be careful)

Edit 2: I wonder if you're running into this issue . What version of Android are you using?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM