service运行在主进程的主线程中
service在manifest中的配置。如果你想让service只在本应用程序中有效(即不被别的应用程序访问到)有两种方法
1:不要配置<intenfilter>,没有<intentfilter>,别的应用程序访问不到你的service,你要访问启动service只能显示启动(intent中明确指出要启动的service的类名)
2:在<service>标签中配置android:exported="false"属性(不论你有没有配置<intentfilter>此service都将是应用程序私有的)。
if your service performs intensive or blocking operations while the user interacts with an activity from the same application,
the service will slow down activity performance. To avoid impacting application performance,you should start a new thread inside the service.
A started service must manage its own lifecycle. That is, the system does not stop or destroy
the service unless it must recover system memory and the service continues to run after onStartCommand() returns.
So, the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().
It's important that your application stops its services when it's done working, to avoid wasting system resources and consuming battery power.
service一旦运行,将在后台一直运行,直到你显示停止service。service一般不会被android回收(内存非常紧张的时候还是会被回收的),
一般activity会优先被回收,实在没办法了,才会考虑回收service。
在service里开启一个线程去执行下载(或者其他的耗时的任务)而不在activity里直接开启一个线程去下载是因为,一旦activity所在的进程被杀死,他的子线程也会被杀死。
而service的生存能力比较强,只要服务不停止(即没显示停止service),则服务所在的进程里就有活动的组件(四大组件,activity,broadcast,service,contentprovider),
这样该进程就不是空进程(没有任何活动组件的进程,空进程容易被杀死,那么正在工作的子线程也会被杀死)则该进程就不会那么容易被android回收。所有service里所开启的线程一般不会被杀死。
1、Service的种类
按运行地点分类:
类别 区别 优点 缺点 应用
本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外Local服务因为是在同一进程因此不需要IPC,也不需要AIDL。相应bindService会方便很多。 主进程被Kill后,服务便会终止。 非常常见的应用如:HTC的音乐播放服务,天天动听音乐播放服务。
远程服务(Remote) 该服务是独立的进程, 服务为独立的进程,对应进程名格式为所在包名加上你指定的android:process字符串。由于是独立的进程,因此在Activity所在进程被Kill的时候,该服务依然在运行,不受其他进程影响,有利于为多个进程提供服务具有较高的灵活性。 该服务是独立的进程,会占用一定资源,并且使用AIDL进行IPC稍微麻烦一点。 一些提供系统服务的Service,这种Service是常驻的。
其实remote服务还是很少见的,并且一般都是系统服务。
按运行类型分类:
类别 区别 应用
前台服务 会在通知一栏显示 ONGOING 的 Notification, 当服务被终止的时候,通知一栏的 Notification 也会消失,这样对于用户有一定的通知作用。常见的如音乐播放服务。
后台服务 默认的服务即为后台服务,即不会在通知一栏显示 ONGOING 的 Notification。 当服务被终止的时候,用户是看不到效果的。某些不需要运行或终止提示的服务,如天气更新,日期同步,邮件同步等。
有同学可能会问,后台服务我们可以自己创建 ONGOING 的 Notification 这样就成为前台服务吗?答案是否定的,前台服务是在做了上述工作之后需要调用 startForeground ( android 2.0 及其以后版本 )或 setForeground (android 2.0 以前的版本)使服务成为 前台服务。这样做的好处在于,当服务被外部强制终止掉的时候,ONGOING 的 Notification 任然会移除掉。
BoundService:
You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications, through interprocess communication (IPC)
Multiple clients can connect to the service at once. However, the system calls your service's onBind()
method to retrieve theIBinder
only when the first client binds. The system then delivers the same IBinder
to any additional clients that bind, without calling onBind()
again.
- A started service
The service is created when another component calls
startService()
. The service then runs indefinitely and must stop itself by callingstopSelf()
. Another component can also stop the service by callingstopService()
. When the service is stopped, the system destroys it.. - A bound service
The service is created when another component (a client) calls
bindService()
. The client then communicates with the service through anIBinder
interface. The client can close the connection by callingunbindService()
. Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. (The service does not need to stop itself.)
These two paths are not entirely separate. That is, you can bind to a service that was already started withstartService()
. For example, a background music service could be started by calling startService()
with an Intent
that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by callingbindService()
. In cases like this, stopService()
or stopSelf()
does not actually stop the service until all clients unbind.