android task kill

后端 未结 5 564
我寻月下人不归
我寻月下人不归 2020-12-11 03:48

I want to kill all tasks that run in android like task killer... What I have done until now is:

ActivityManager manager =  (ActivityManager) this.getSystemSe         


        
相关标签:
5条回答
  • 2020-12-11 04:04

    You're using 0 (zero) instead of i inside your loop.

    for (int i = 0; i < activityes.size(); i++){
    
        Log.e("APP: "+i, activityes.get(i).processName);
    
        if (!activityes.get(i).processName.equals("app.android.myapp")){
            Process.killProcess(activityes.get(i).pid);
        }
    
    }
    

    Cheers

    0 讨论(0)
  • 2020-12-11 04:05

    You can kill the current process on back pressed using the following code:

    public void onBackPressed() {
        super.onBackPressed();
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
    
    0 讨论(0)
  • 2020-12-11 04:10

    You do not have the rights to kill other processes; hence, killProcess() does not work for your app.

    0 讨论(0)
  • 2020-12-11 04:15

    You can try this to kill your task or app:

    ActivityManager am = (ActivityManager) ctx
                    .getSystemService(ctx.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(packageName);
    

    this works for 2.2 and up.

    0 讨论(0)
  • 2020-12-11 04:18

    1- Add to manifest

    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    

    2 - In your code

    Runtime.getRuntime().exec("adb shell killall com.example.app");
    

    note that your app need to have access to adb shell (system app)

    0 讨论(0)
提交回复
热议问题