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

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

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

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

希望这会有所帮助.

更多推荐

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

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

发布评论

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

>www.elefans.com

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