如何检测应用程序从最近列表中删除

编程入门 行业动态 更新时间:2024-10-13 22:23:18
本文介绍了如何检测应用程序从最近列表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我工作的一个音乐应用程序,我想停止服务时,用户从最近的列表中删除该应用程序,而不是在第一个活动被销毁(因为当用户单击后退后退,直到应用程序最小化,在这种情况下,第一活动也被破坏掉了)。请帮助。

解决方案   

我要停止该服务时,用户从申请  最近列表。

是的,您可以做,使用 stopWithTask 标记为真正为服务清单文件。

示例:

<服务    机器人:启用=真    机器人:为MyServiceNAME =    机器人:出口=假    机器人:stopWithTask =真/>

如果你需要应用程序的事件最近名单中移除,并停止服务之前做一些事情,你可以用这样的:

<服务    机器人:启用=真    机器人:为MyServiceNAME =    机器人:出口=假    机器人:stopWithTask =FALSE/>

所以,你的服务的方法 onTaskRemoved 将被调用。 (记住的,它不会,如果你设置一个名为 stopWithTask 到真正)。

公共类的MyService扩展服务{    @覆盖    公共无效onStartService(){        //你的code    }    @覆盖    公共无效onTaskRemoved(意向rootIntent){        的System.out.println(onTaskRemoved称为);        super.onTaskRemoved(rootIntent);        //做你想要的东西        //一站式服务        this.stopSelf();    }}

希望这有助于。

I'm working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when user clicks back back until app minimizes, in that case first activity is also getting destroyed). Please help.

解决方案

I want stop the service when user removes the application from the recent list.

Yes, you can either do that using stopWithTask flag as true for Service in Manifest file.

Example:

<service android:enabled="true" android:name=".MyService" android:exported="false" android:stopWithTask="true" />

OR

If you need event of application being removed from recent list, and do something before stopping service, you can use this:

<service android:enabled="true" android:name=".MyService" android:exported="false" android:stopWithTask="false" />

So your service's method onTaskRemoved will be called. (Remember, it won't be called if you set stopWithTask to true).

public class MyService extends Service { @Override public void onStartService() { //your code } @Override public void onTaskRemoved(Intent rootIntent) { System.out.println("onTaskRemoved called"); super.onTaskRemoved(rootIntent); //do something you want //stop service this.stopSelf(); } }

Hope this helps.

更多推荐

如何检测应用程序从最近列表中删除

本文发布于:2023-11-27 10:47:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!