How can I kill processes in Android?

梦想的初衷 提交于 2019-11-29 05:17:14
  1. The message you're getting indicates that your app has crashed. You need to look at its LogCat to find out why. This is documented in the Android Developer Guide.
  2. Please state your reason for killing all background processes, because I can't think of any appropriate reason to do this. People persist in claiming that "task killers" or "app killers" improve performance, but this attitude ignores the real problem: poorly-written apps. As long as we continue to claim that task killers help, users will continue to use apps that leave unnecessary services, etc. running. Forcing users to use task killers is like dealing with a termite problem by killing one termite at a time as you see them. The real answer is to exterminate all the termites.

In short, anyone who shows you how to kill all background processes is doing you a disservice and the Android community a disservice.

I use this code to kill my own process (app) :

    android.os.Process.killProcess(android.os.Process.myPid());
ObAt

You can use this code:

List<ApplicationInfo> packages;
    PackageManager pm;
    pm = getPackageManager();
    //get a list of installed apps.
    packages = pm.getInstalledApplications(0);

    ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

   for (ApplicationInfo packageInfo : packages) {
        if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
        if(packageInfo.packageName.equals("mypackage")) continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
   }  

Source

Keep in mind that it's very dangerous to kill apps. If you don't exactly know what you're doing, don't use this code please!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!