如何确保服务不会被杀死 [英] how to make sure service is not killed

查看:88
本文介绍了如何确保服务不会被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android中编写音乐播放器,并试图使用服务在后台无限循环播放音乐.我需要该服务与活动进行交互以进行音量控制等.问题是,如果我使用服务绑定程序,那么当活动处于后台时,服务很可能会随活动终止.如何确保服务可以在没有活动的情况下继续运行,并且在活动中仍然可以调用该服务的某些方法?

I am trying to write a music player in Android, and was trying to use service to play music in infinite loop in background. And I need the service to interact with the activity for volume control and so on. The question is, if I use service binder then the service will likely to be terminated with the activity when the activity is in the background. How to make sure the service can keep running without the activity and in activity some of the service's method can still be called?

将前景服务与活页夹一起使用是否是解决方案?服务不会以这种方式被杀死吗?

Is using foreground service with binder a solution? The service will not be killed in this way?

提前谢谢!

推荐答案

Per this process priority blog post, the only thing a higher priority than a foreground service is the foreground activity. This means that a foreground service will almost never be killed.

根据建立音频应用服务文档:

正在播放服务时,它应该在前台运行.这使系统知道该服务正在执行有用的功能,并且如果系统内存不足,则不应终止该服务.前台服务必须显示通知,以便用户知道它并可以选择控制它.的onPlay()回调应将服务置于前台.

When a service is playing, it should be running in the foreground. This lets the system know that the service is performing a useful function and should not be killed if the system is low on memory. A foreground service must display a notification so the user knows about it and can optionally control it. The onPlay() callback should put the service in the foreground.

这篇关于如何确保服务不会被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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