本文介绍了是否有可能关闭的应用程序编程这是在后台运行的机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止这是在后台运行在Android或关闭应用程序。可能吗?如果是的话如何实现这一目标。请参阅任何链接将AP preciated。

I want to stop or close apps which are running in background in android. Is it possible? If so how to achieve this. Refer any links will be appreciated.

在此先感谢。

推荐答案

您可以使用 Process.killProcess(INT PID)杀有相同的UID与进程的应用程序。您可以在清单中使用 ActivityManager.kil​​lBackgroundProcesses(字符串的packageName),与KILL_BACKGROUND_PROCESSES 许可(对于API> = 8)或者 ActivityManager.restartPackage(字符串的packageName )(对于API< 8)杀死指定的进程,forground过程中,除了

You can use Process.killProcess(int pid) to kill processes that have the same UID with your App.You can use ActivityManager.killBackgroundProcesses(String packageName),with KILL_BACKGROUND_PROCESSES permission in your manifest(for API >= 8) or ActivityManager.restartPackage (String packageName)(for API < 8) to kill specified process,except of forground process.

所以,如果你会杀死所有其他进程时,你的程序是前台的过程中,你会使用 ActivityManager.kil​​lBackgroundProcesses ActivityManager.restartPackage

So if you would to kill all other processes when your program is foreground process,you would to use ActivityManager.killBackgroundProcesses or ActivityManager.restartPackage:

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);
   }   

在上面的代码中code,每道工序都会被杀死,除非它是你的应用程序或系统进程的过程。

In above snippet code,each process will be killed unless it be process of your App or system process.

这篇关于是否有可能关闭的应用程序编程这是在后台运行的机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 17:21