除了 android.os.Process.killProcess 和 Sytem.exit(0) 之外,还有什么方法可以终止我的应用程序?

发布于 2024-12-19 02:57:59 字数 263 浏览 2 评论 0原文

我的应用程序有问题。当用户单击“退出”时,我真的需要杀死它。我已经尝试了问题标题中列出的两种方法,问题是:我有一个持久(前台)服务正在执行工作,因此我的应用程序有一个托盘图标。当用户通过此图标切换到我的应用程序然后按“退出”时 - 该应用程序被终止,但 Android 立即重新启动它。如果用户使用任何其他方法(不是服务图标)切换回应用程序 - 应用程序在退出时不会重新启动。

那么,可靠的查杀应用程序的方法是什么呢?

PS 应用程序中始终只有一项活动(如果该活动很重要的话)。

I have a problem with my app. I really need to kill it when user clicks "Exit". I've tried both methods listed in the question header, and here's the problem: I have a persistent (foreground) service performing work, so there is a tray icon for my app. When user switches to my app via this icon and then presses "Exit" - the app is kill, but Android immediately restarts it. If the users switches back to the app using any other method (not the service icon) - application is not restarted upon exit.

So, what is the reliable way of killing application?

P. S. There's always only one activity in the application, if that is of any importance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

德意的啸 2024-12-26 02:58:00

对于只有一个 Activity,我选择 finish()

for only one Activity, I choose finish().

花开浅夏 2024-12-26 02:58:00

您可以使用以下意图退出您的活动

Intent startMain = new Intent(Intent.ACTION_MAIN);
          startMain.addCategory(Intent.CATEGORY_HOME);
          startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(startMain);

,并在调用此意图之前调用 finish() 。

You can use following intent to exit from your activity

Intent startMain = new Intent(Intent.ACTION_MAIN);
          startMain.addCategory(Intent.CATEGORY_HOME);
          startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(startMain);

and Call finish() before you call this intent.

无妨# 2024-12-26 02:58:00

我已经找到了解决我的问题的方法。如果我使用 moveTaskToBack(true); 将活动移动到后台,然后终止该进程,则它不会重新启动。

I have found the solution to my problem. If I move my activity to background with moveTaskToBack(true); and then kill the process it is not restarted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文