有没有方法可以清除应用程序(Android)中的所有任务? [英] Is there a method to clear all the task in an app (Android)?

查看:122
本文介绍了有没有方法可以清除应用程序(Android)中的所有任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题,只想知道我的项目中是否有2个任务.有没有办法一次性清除它们.这是因为当我尝试清除两项任务以退出我的应用程序时,只有一项正在清除,而另一项仍在运行,这阻止了我退出该应用程序.

Quick question, just want to know if i have 2 tasks in my project. Is there any way to clear both of them in a single go. This is because when i try to clear both task to quit my app, only one is clearing and the another one is still alive which prevents me from quitting the application.

finishAffinity();
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

有什么建议解决这个问题吗?

Any suggestions to solve this ?

推荐答案

如果您确实要退出这两个任务,则需要这样做:

If you really want to quit both tasks, then you need to do it like this:

当您要退出时,您发送ACTION设置为"my.package.name.QUIT"之类的广播Intent.

When you want to quit, you send a broadcast Intent with the ACTION set to something like "my.package.name.QUIT".

在您的所有活动中,声明并注册一个侦听器,如下所示:

In all of your activities, declare and register a listener, like this:

registerReceiver(myReceiver, new IntentFilter("my.package.name.QUIT"));

调用BroadcastReceiver中的onReceive()时,应调用finish().

这将确保您完成所有活动,无论它们在何处运行.

This will ensure that all of your activities finish, regardless of where they are running.

注意:我仍然不明白为什么您要执行多个任务,并且我认为您需要解决该问题.这里的解决方案实际上只是一个hack,因为您可能不想执行多个任务.

这篇关于有没有方法可以清除应用程序(Android)中的所有任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆