简体   繁体   中英

How to kill android application running in background process

I have an application with call activity, when I finish action in activity the activity is still running in background device. Now I want to stop the activity and not allow it to run in the background when it is finished. I'm also using killBackgroundProcesses but it's not working.

public void KillApplication(String killPackage) {
    ActivityManager am =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    android.os.Process.killProcess(runningProcess.pid);
    am.killBackgroundProcesses(killPackage);
}

I just call finish(); and then I use the function KillApplication("mypackage"); .

From the looks of it, your processes should be terminated. Are you thinking that the process is still running because the application still appears in the 'recents' list? If that's the case, take a look at the "Close application and remove from recent apps" question.

It explains how to remove an application from the recents list by using an Intent flag.

What do you mean by run in background? The best practice is calling finish(), make sure all the process are stop, and let the OS fully kill the app when it need the resource.

But if you want to force kill, you can use System.exit(1) instead of finish() .

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