简体   繁体   English

如何杀死在后台进程中运行的android应用程序

[英]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. 我也在使用killBackgroundProcesses但是它不起作用。

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(); 我只是打电话给finish(); and then I use the function KillApplication("mypackage"); 然后我使用功能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. 它说明了如何通过使用Intent标志从最新列表中删除应用程序。

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. 最佳实践是调用finish(),确保所有进程都已停止,并让OS在需要资源时完全杀死该应用程序。

But if you want to force kill, you can use System.exit(1) instead of finish() . 但是,如果要强制杀死,则可以使用System.exit(1)而不是finish()

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

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