简体   繁体   English

从另一个应用程序完成或关闭一个应用程序

[英]finish or close an application from another application

I want to know, is it possible to finish or close an running application from my application? 我想知道,是否可以从应用程序中完成或关闭正在运行的应用程序? If yes then how ? 如果是,那怎么办?

I googled regarding this but not found any solution. 我对此进行了谷歌搜索,但未找到任何解决方案。 I tried using killBackgroundProcesses but it also not worked for me. 我尝试使用killBackgroundProcesses但它对我也不起作用。 The functionality what I am expecting something like Task Manager is doing. 我期望任务管理器正在执行的功能。 Please suggest me some thing. 请给我一些建议。 Thanks in advance. 提前致谢。

No one can kill process except Android OS itself. 除了Android OS本身,没有人可以杀死进程。

Most of the task killer in android market don't kill the app they just restart the process android市场中的大多数任务杀手都不会杀死应用程序,他们只是重新启动该过程

by using 通过使用

public void restartPackage (String packageName) when this method is called by your activity the operating system immediately called public void restartPackage(字符串packageName),当您的活动调用该方法时,操作系统立即调用该方法

savedInstanceState and save the state of that activity you want to kill. savedInstanceState并保存您要终止的活动的状态。 Now this process is 现在这个过程是

removed from memory and OS saved it state.Now when next time user start that activity it 从内存中删除,操作系统保存了它的状态。现在,当用户下次启动该活动时

will start from where it was killed or in other words restarted. 从被杀死或换句话说重新开始的地方开始。 You can verify it from any 您可以从任何

task manager that they don't kill the process because no one can do so. 任务管理者,他们没有杀死进程,因为没人能这样做。 This method also 这种方法也

work in ICS. 在ICS中工作。

for above method you can look at here . 对于上述方法,您可以在这里查看 As far as i know killBackgroundProcesses (String packageName) is for API 8 and above. 据我所知,killBackgroundProcesses(字符串packageName)适用于API 8及更高版本。

First, you need a pid of process. 首先,您需要一点流程。 You can get it for the package name like in this code: 您可以像下面的代码那样获得它的包名称:

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
             int processid = 0;
       for(int i = 0; i < pids.size(); i++)
       {
           ActivityManager.RunningAppProcessInfo info = pids.get(i);
           if(info.processName.equalsIgnoreCase("here your package name")){
              processid = info.pid;
           } 
       }

Then you can use killProcess function for terminate process with specified pid. 然后,您可以使用killProcess函数终止具有指定pid的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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