安卓停止服务时,应用程序与任务管理器关闭 [英] Stop Android service when app is closed with the task manager

查看:130
本文介绍了安卓停止服务时,应用程序与任务管理器关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与前景的服务,对于延长的时间周期的音频播放一个应用程序。

I am working on an app with a foreground service that plays audio for an extended period of time.

我不希望Android操作系统杀死我的服务,同时它在后台运行,但是,我想停止该服务,如果应用程序从任务管理器中手动关闭。

I do not want the Android OS to kill my service while it's running in the background, however I want to stop the service if the app is manually closed from the task manager.

我的应用程序的当前行为是服务继续当应用程序在任务管理器封闭运行。

The current behavior of my app is that the services continues to run when the app is closed in the task manager.

我已经注意到,Spotify的达到预期的效果,它是如何做到的呢?

I've noticed that Spotify achieves the desired effect, how is it accomplished?

推荐答案

我认为,你可以叫 stopSelf() <一个href="http://developer.android.com/reference/android/app/Service.html#onTaskRemoved%28android.content.Intent%29"相对=nofollow>从 onTaskRemoved() 。还有一个严重underdocumented <一href="http://developer.android.com/reference/android/content/pm/ServiceInfo.html#FLAG_STOP_WITH_TASK"相对=nofollow> 安卓stopWithTask 你显然可以有&LT上;服务&GT; 元素当任务从最近的任务列表中停下来自动停止该服务。

I think that you can call stopSelf() from onTaskRemoved(). There's also a severely underdocumented android:stopWithTask you apparently can have on the <service> element to automatically stop the service when the task is stopped from the recent-tasks list.

请注意,我没有尝试过任何这些,所以因人而异。

Note that I haven't tried either of these, so YMMV.

这篇关于安卓停止服务时,应用程序与任务管理器关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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