我工作的一个音乐应用程序,我想停止服务时,用户从最近的列表中删除该应用程序,而不是在第一个活动被销毁(因为当用户单击后退后退,直到应用程序最小化,在这种情况下,第一活动也被破坏掉了)。请帮助。
解决方案我要停止该服务时,用户从申请 最近列表。
是的,您可以做,使用 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.
更多推荐
如何检测应用程序从最近列表中删除
发布评论