如何检测从最近列表中删除的应用程序? [英] How to detect app removed from the recent list?
问题描述
我正在开发一个音乐应用程序,我想在用户从最近的列表中删除该应用程序时停止该服务,而不是在第一个活动被销毁时停止该服务(因为用户单击后退直到应用程序最小化,在这种情况下,首先活动也被破坏了).请帮忙.
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.
是的,您可以使用清单文件中Service的 stopWithTask
标志作为 true
来做到这一点.
Yes, you can either do that using stopWithTask
flag as true
for Service in Manifest file.
示例:
<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" />
因此将调用服务的方法 onTaskRemoved
.(记住,如果将 stopWithTask
设置为 true
,则不会调用它.)
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();
}
}
希望这会有所帮助.
这篇关于如何检测从最近列表中删除的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!