简体   繁体   English

如何从最近的应用程序列表中删除应用程序?

[英]How to remove application from recent application list?

I guess that Android won't let people to do this, because they think they have perfect handle for the task/applications.我猜 Android 不会让人们这样做,因为他们认为他们对任务/应用程序有完美的处理能力。 However, I really need to do this in my case.但是,在我的情况下,我确实需要这样做。

I have an activity A acting as the entry point of my application.我有一个活动 A 作为我的应用程序的入口点。 In that activity, it reads the preference and decided which activity to start, say B or C. After that, it finishes itself.在那个活动中,它读取偏好并决定开始哪个活动,比如说 B 或 C。之后,它自己完成。 So, activity A never appears to the users.因此,活动 A 永远不会出现在用户面前。

My application stores things on sdcard, and reads from it constantly.我的应用程序将内容存储在 SD 卡上,并不断从中读取。 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.因此,当卸载 sdcard 时,我需要向用户显示一条消息,表明该 sdcard 不可用,而不是打开 B 或 C。我设置了检查 A 以在 sdcard 不可用时显示该消息。 When that message is displayed, A will not try to start B or C.显示该消息时,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.我可以简单地将我的支票移到 B 和 C 来解决这个问题。 But in the future, the number of activities started from A will increase.但在未来,从A开始的活动数量会增加。 If there are 6 of them, I'll need to copy this check to 6 places.如果有 6 个,我需要将此支票复制到 6 个位置。 Needless to say, this looks very ugly, and is a maintenance nightmare.不用说,这看起来非常难看,而且是维护的噩梦。

So, the best fix should be removing my application from recent application list when the sdcard is uunmounted.因此,最好的解决方法应该是在卸载 sdcard 时从最近的应用程序列表中删除我的应用程序。 However, I can't find how to do this.但是,我找不到如何执行此操作。 Even killing the process or use ActivityManager.restartPackage, it still appears in the list.即使杀死进程或使用ActivityManager.restartPackage,它仍然出现在列表中。 Can anyone tell me how to remove it from the list?谁能告诉我如何从列表中删除它?

try试试

<activity android:name=".MainActivity"
        android:excludeFromRecents="true" ...

in your AndroidManifest.xml 's activity declaration.在您的AndroidManifest.xml的活动声明中。

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">

只需在清单文件的活动标签中添加android:excludeFromRecents="true" ..很简单

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.将标签设置为空将使此活动不会显示在最近的应用程序列表中。

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 :AndroidManifest向 B 和 C 添加以下内容:

android:noHistory= "true"

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.一个简单的方法可能是创建一个执行 SD 卡检查的基本活动,并从它扩展所有活动。 That way the check is only in one place.这样支票只在一个地方。

OK, I know for a fact that it can be done in 2.3.4.好的,我知道它可以在 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.打开应用程序 Toddler Lock 时会清除最近的应用程序列表,因此当您“锁定”手机时,如果您长按主页键,列表将变为空白。 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.因此,对于正在查看和阅读 postf 的人来说,这不可能,不要放弃。 I sure heck am not.我确定不是。

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);

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()
        }
    }
}

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

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