Android 如何从最近的应用程序列表中删除应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3762763/
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
How to remove application from recent application list?
提问by user412759
I guess that Android won't let people to do this, because they think they have perfect handle for the task/applications. However, I really need to do this in my case.
我猜 Android 不会让人们这样做,因为他们认为他们对任务/应用程序有完美的处理能力。但是,在我的情况下,我确实需要这样做。
I have an activity A acting as the entry point of my application. In that activity, it reads the preference and decided which activity to start, say B or C. After that, it finishes itself. So, activity A never appears to the users.
我有一个活动 A 作为我的应用程序的入口点。在那个活动中,它读取偏好并决定开始哪个活动,比如说 B 或 C。之后,它自己完成。因此,活动 A 永远不会出现在用户面前。
My application stores things on sdcard, and reads from it constantly. So, when the sdcard is unmounted, I need to display a message to the user that the sdcard is unavailable, instead of opening B or C. I set a check in A to display that message when sdcard is unavilable. When that message is displayed, A will not try to start B or C.
我的应用程序将内容存储在 SD 卡上,并不断从中读取。因此,当卸载 sdcard 时,我需要向用户显示一条消息,表明该 sdcard 不可用,而不是打开 B 或 C。我设置了检查 A 以在 sdcard 不可用时显示该消息。显示该消息时,A 将不会尝试启动 B 或 C。
Things works perfectly if user only enter my application from application launcher. However, I found that user can also enter my application by long pressing home and choose it from the recent application list, if he has opened it recently. When user does that, it skips A and goes directly to B or C. I don't have the check in both of them, so exception is thrown while I am trying to access sdcard, and force close dialog pops up.
如果用户仅从应用程序启动器输入我的应用程序,则一切正常。但是,我发现用户也可以通过长按主页进入我的应用程序并从最近的应用程序列表中选择它,如果他最近打开过它。当用户这样做时,它会跳过 A 并直接转到 B 或 C。我没有检查它们,因此在我尝试访问 SD 卡时抛出异常,并弹出强制关闭对话框。
I can simply move my check to both B and C to fix this problem. But in the future, the number of activities started from A will increase. If there are 6 of them, I'll need to copy this check to 6 places. Needless to say, this looks very ugly, and is a maintenance nightmare.
我可以简单地将我的支票移到 B 和 C 来解决这个问题。但在未来,从A开始的活动数量会增加。如果有 6 个,我需要将此支票复制到 6 个位置。不用说,这看起来非常难看,而且是维护的噩梦。
So, the best fix should be removing my application from recent application list when the sdcard is uunmounted. However, I can't find how to do this. Even killing the process or use ActivityManager.restartPackage, it still appears in the list. Can anyone tell me how to remove it from the list?
因此,最好的解决方法应该是在卸载 sdcard 时从最近的应用程序列表中删除我的应用程序。但是,我找不到如何执行此操作。即使杀死进程或使用ActivityManager.restartPackage,它仍然出现在列表中。谁能告诉我如何从列表中删除它?
回答by whlk
try
尝试
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
in your AndroidManifest.xml
's activity declaration.
在您AndroidManifest.xml
的活动声明中。
回答by Kislingk
Other attributes can help your activity isolate from other activities in the same package.
其他属性可以帮助您的活动与同一包中的其他活动隔离。
<activity
android:name=".aActivity"
android:excludeFromRecents="true"
android:taskAffinity=""
android:launchMode="singleInstance">
回答by user632905
just add android:excludeFromRecents="true"
in your activity's tag in the manifest file..its easy
只需android:excludeFromRecents="true"
在清单文件中添加您的活动标签..很简单
回答by Hymanylalala
You need to set below code in Launcher Activity in AndroidManifest.xml :
您需要在 AndroidManifest.xml 的 Launcher Activity 中设置以下代码:
<activity>
...
android:excludeFromRecents="true"
...
</activity>
or start this activity from
或从
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
and also in AndroidManifest.xml
并且也在 AndroidManifest.xml 中
<activity>
...
android:label=""
...
</activity>
Setting label as empty will let this activity NOT be shown in Recent App list.
将标签设置为空将使此活动不会显示在最近的应用程序列表中。
回答by Macarse
I would not recommend to do so but I would try the following options:
我不建议这样做,但我会尝试以下选项:
Option 1:
选项1:
On B and C add:
在 B 和 C 上添加:
protected void onPause() {
finish();
}
Option 2:
选项 2:
Add to B and C the following in the AndroidManifest
:
在 B 和 C 中添加以下内容AndroidManifest
:
android:noHistory= "true"
回答by Cheryl Simon
Removing your application from the recent apps list is probably not possible, and definitely not the best solution. That will just confuse the user who expects all apps to behave similarly.
从最近的应用程序列表中删除您的应用程序可能是不可能的,而且绝对不是最好的解决方案。这只会让期望所有应用程序行为相似的用户感到困惑。
Regardless, I don't think it will solve your problem. If the user hits home while on Activity B, then selects your app from the home page, it will start Activiy B again.
无论如何,我认为它不会解决您的问题。如果用户在 Activity B 上点击主页,然后从主页选择您的应用程序,它将再次启动 Activiy B。
There are a number of ways to solve the real problem. One easy one might be to create a base Activity that performs the SD card check, and have all of your activities extend from it. That way the check is only in one place.
有多种方法可以解决实际问题。一个简单的方法可能是创建一个执行 SD 卡检查的基本 Activity,并使您的所有 Activity 从中扩展。这样支票只在一个地方。
回答by MastaCarlos
OK, I know for a fact that it can be done in 2.3.4. The app Toddler Lock when opened clears the recent app list so that when you "Lock" your phone if you long press home key the list is blank. Unfortunately I have not found how to do it. So for anyone who is looking and reading postf that say it is not possible don't give up. I sure heck am not.
好的,我知道它可以在 2.3.4 中完成。打开应用程序 Toddler Lock 时会清除最近的应用程序列表,因此当您“锁定”手机时,如果您长按主页键,列表将变为空白。不幸的是,我还没有找到如何去做。因此,对于正在查看和阅读 postf 的人来说,这不可能,不要放弃。我确定不是。
回答by Tushar Saha
try this, it will finish all activities and remove app from recents
试试这个,它将完成所有活动并从最近的应用程序中删除应用程序
private fun killCurrentApp() {
val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val appTasks = am!!.getAppTasks()
if (appTasks.size > 0) {
val appTask = appTasks.get(0)
appTask.finishAndRemoveTask()
}
}
}
回答by Mahmouf Mrad
if you wanna exit Application on Button click use this code :
如果您想在按钮上退出应用程序,请使用以下代码:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.
要杀死完整的应用程序并将其从 Runningapp 列表中删除,请通过其 pid(其讨厌的)杀死该应用程序...在上述代码之前使用此行。
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);