(安卓后台服务)-编程知识网

android services是什么,是否可以卸载了?

  android services是后台服务,系统服务没办法卸载,但可能被关闭。

  

1、系统服务是维持系统正常运行的基础,强制卸载或者关闭,可能造成系统异常,无限重启,或者变成砖头。  

2、应用软件也可以开启Services,但是用户应用的服务,在应用被卸载后,也会被卸载,且优先级比系统的低,在系统资源不足的情况下可能被关闭。

安卓,后台程序过多,会不会影响耗电?

后台程序一般处于停止状态,会占内存,但是便于下次快速启动,耗电不大,这是Android系统的一个特色,但是,后台服务一直处于运行状态,那绝对是耗电的。后台服务是一直运行的程序,后台程序是暂时停止运行但驻留在内存中的程序。

Android服务指的是什么?

Android服务Service是android系统中的一种组件,安卓不同版本图片它跟Activity的级别差不多,但是他不能自己运行,只能后台运行,并且可以和其他组件进行交互!

服务( Service )是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使当程序被切换到后台,或者用户打开了另外一个应用程序,服务仍然能够保持正常运行。

怎么设置让一个软件一直在后台运行?

android 一直运行的后台服务是不存在的,而且也不是最佳实践,因为一直运行的后台服务会耗费大量系统资源,影响其他程序的响应从而影响到用户体验。
题主问的是如何让后台服务一直运行,我认为只有系统自带的应用或者定制的系统应用才可以有这么高的优先级可以保持后台服务一直运行。如果是在非root的系统上的普通应用,只能是通过一些方法,让用户觉着后台服务一直运行。比如,监听开机事件,显式地启动后台服务;启动后台服务后给它设置“前台运行”的优先级;定时任务来检查后台服务是否在运行,不运行的话重新启动它。
可以考虑使用如下几种方案来达到一直运行的效果。
1. 调用startForeground方法,

android: Service vs SingleTop Activity moved to background

2. 使用AlarmManager 发送定时任务 :

Diamonds Are Forever. Services Are Not.

更极端的例子,如果应用被干掉了,定时任务(AlarmManager)这种方法确实不管用了,但是可以考虑给后台服务设置“前台”运行的优先级这种方法。比如音乐播放器,在启动播放服务后,通知栏会显示一个播放进度的通知条,这个通知条是必须的,因为通过它才能使后台服务获取到“前台”运行的优先级从而避免被系统干掉。这种做法也是官方推荐的做法。
下面是官方文档中的一些具体说明:

说明一

A started service can use the

startForeground(int, Notification)

API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

说明二

Service | Android Developers

(int, android.app.Notification)

Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.

其他参考资料

使用startForeground让android服务前台运行

android – startForeground() without showing notification