Android 关闭应用程序并从最近的应用程序中删除/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22166282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Close application and remove from recent apps/
提问by Hamad
I know this question is common and asked many times on Stack Overflow, but after visiting nearly the four pages of search engine results and nearly the 20 question of Stack Overflow regarding this issue, I found that none of them is resolved or answered correctly.
我知道这个问题很常见,在 Stack Overflow 上被问过很多次,但是在访问了将近四页的搜索引擎结果和 Stack Overflow 上关于这个问题的近 20 个问题之后,我发现它们都没有得到解决或正确回答。
What I want:
我想要的是:
I want to show my app in recent apps list when it is running but when I close app then my process should be killed and application should be removed from recent apps list.
我想在运行时在最近的应用程序列表中显示我的应用程序,但是当我关闭应用程序时,我的进程应该被终止,并且应用程序应该从最近的应用程序列表中删除。
Some answers I found:
我找到的一些答案:
use System.exit(0); //doesn't clear app from recents
OR
use android.os.Process.killProcess(android.os.Process.myPid()); //doesn't clear app from recents
OR
use finish() or this.finish() or Activity.finish();// doesn't clear app from recents
And one common suggestion I see in every answer that add below code in manifest:
我在每个答案中看到的一个常见建议是在清单中添加以下代码:
android:excludeFromRecents //I think this is wrong approach.
because after adding this when user presses home button while my app is running then user cannot see my app in recent Apps List.
因为在我的应用程序正在运行时用户按下主页按钮添加此后,用户无法在最近的应用程序列表中看到我的应用程序。
And many other suggestions on this but none of them do both tasks close application and clear application from recent apps list. Also if you go in Settings>Apps>yourApp>your Application
see that still it asks for "force stop" means application is running!
以及许多其他关于此的建议,但它们都没有完成关闭应用程序和从最近的应用程序列表中清除应用程序的任务。此外,如果您进去Settings>Apps>yourApp>your Application
看到它仍然要求“强制停止”,则表示应用程序正在运行!
回答by Luke
I based my solution on guest's above, as well as gilsaints88's comments below (for Android L compatibility):
我的解决方案基于上面的客人,以及下面的 gilsaints88 评论(对于 Android L 兼容性):
Add this activity to your AndroidManifest.xml file:
将此活动添加到您的 AndroidManifest.xml 文件中:
<activity
android:name="com.example.ExitActivity"
android:theme="@android:style/Theme.NoDisplay"
android:autoRemoveFromRecents="true"/>
Then create a class ExitActivity.java:
然后创建一个类ExitActivity.java:
public class ExitActivity extends Activity
{
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT >= 21)
{
finishAndRemoveTask();
}
else
{
finish();
}
}
public static void exitApplication(Context context)
{
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
Then whenever you want to force close your application and remove it from the recent's list, call:
然后,每当您想强制关闭您的应用程序并将其从最近的列表中删除时,请调用:
ExitActivity.exitApplication(context);
This solution works for me instead of declaring android:excludeFromRecents="true" because I want the user to be able to see the app in the recents list UNTIL the point where my app triggers closing the app programmatically.
这个解决方案适用于我而不是声明 android:excludeFromRecents="true" 因为我希望用户能够在最近列表中看到该应用程序,直到我的应用程序触发以编程方式关闭该应用程序的点。
回答by guest
Before I present my attempt at this, I want to make it clear that what follows won't address the availability of "force stop" in the application info. Android allows you to force stop an application even if it does not have any processes running. Force stop puts the package into a specific stopped state, where it can't receive broadcast events.
在我提出我的尝试之前,我想明确说明以下内容不会解决应用程序信息中“强制停止”的可用性问题。Android 允许您强制停止应用程序,即使它没有任何进程正在运行。强制停止将包置于特定的停止状态,在该状态下它无法接收广播事件。
Now that that's out of the way, on with my cheesy idea for this. The gist of it is to replace the task that you want to exit with one that the system will exclude from the recent apps list.
现在那已经不碍事了,继续我对此的俗气想法。它的要点是将您要退出的任务替换为系统将从最近的应用程序列表中排除的任务。
- Define an activity (I'll call it
Exiter
) that just finishes itself immediately.- Call
finish()
inonCreate(Bundle)
. - From the question, it sounds like the OP will also want to call
System.exit(int)
too. But for other people reading this answer, you usually don't have to kill the process. - Use
android:theme="@android:style/Theme.NoDisplay"
to prevent this from causing a bunch of redundant transitions.
- Call
- Set
android:taskAffinity
so thatExiter
will run in the task that you want to exit. Or don't: by default, unspecifiedtaskAffinity
makes it so that all activities share a common anonymoustaskAffinity
. - When you want to exit, use
startActivity(Intent)
to startExiter
with the following flags:Intent.FLAG_ACTIVITY_NEW_TASK
- Even though we want to start the activity in the same task, the other flags require this flag. Fortunately, thetaskAffinity
will prevent the system from actually starting a new task.Intent.FLAG_ACTIVITY_CLEAR_TASK
- This finishes all the activities on the task. You won't have to callfinish()
everywhere, just inExiter
.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
- The task won't be in the recent apps list. Since it's the same task as the one that's exiting, this flag removes the task from the list.
- 定义一个
Exiter
立即完成的活动(我称之为)。- 打
finish()
进来onCreate(Bundle)
。 - 从这个问题来看,听起来 OP 也想打电话
System.exit(int)
。但是对于阅读此答案的其他人,您通常不必终止该过程。 - 使用
android:theme="@android:style/Theme.NoDisplay"
以避免因此导致了一堆多余的转换。
- 打
- 设置
android:taskAffinity
为Exiter
将在您要退出的任务中运行。或者不要:默认情况下, unspecifiedtaskAffinity
使得所有活动共享一个公共的匿名taskAffinity
. - 当您想退出时,使用以下标志
startActivity(Intent)
开始Exiter
:Intent.FLAG_ACTIVITY_NEW_TASK
- 即使我们想在同一个任务中启动活动,其他标志也需要这个标志。幸运的是,这taskAffinity
会阻止系统实际启动新任务。Intent.FLAG_ACTIVITY_CLEAR_TASK
- 这完成了任务的所有活动。您不必finish()
到处打电话,只需在Exiter
.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
- 该任务不会出现在最近的应用列表中。由于它与正在退出的任务相同,因此此标志将从列表中删除该任务。
回答by Benny
Add this to your Activity:
将此添加到您的活动:
@Override
public void finish() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
super.finishAndRemoveTask();
}
else {
super.finish();
}
}
Call this when your activity is done and should be closed and the task should be completely removed as a part of finishing the root activity of the task.
当您的活动完成并且应该关闭并且任务应该作为完成任务的根活动的一部分被完全删除时调用它。
回答by Mahesh Kartan
finishAndRemoveTask();
worked for me to remove an app from taskbar. After closing app.
finishAndRemoveTask();
为我工作从任务栏中删除了一个应用程序。关闭应用程序后。
回答by Alexandr
The solution for Kotlin.
Kotlin 的解决方案。
ExitHelper.kt
退出助手.kt
fun Activity.finishAndRemoveTaskCompat() {
if (android.os.Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask()
} else {
val intent = Intent(this, ExitAndRemoveFromRecentAppsDummyActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK or
Intent.FLAG_ACTIVITY_NO_ANIMATION or
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
startActivity(intent)
}
}
class ExitAndRemoveFromRecentAppsDummyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
finish()
}
}
AndroidManifest.xml
AndroidManifest.xml
<activity
android:name=".ExitAndRemoveFromRecentAppsDummyActivity"
android:theme="@android:style/Theme.NoDisplay"
/>
Then just use it as a usual method in your Activity:
然后将其用作您的 Activity 中的常用方法:
override fun boo() {
foo()
finishAndRemoveTaskCompat()
}