如何检测应用程序从最近列表中删除 [英] How to detect app removed from the recent list

查看:1186
本文介绍了如何检测应用程序从最近列表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


解决方案

  

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


是的,您可以做,使用 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.

这篇关于如何检测应用程序从最近列表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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