service想必在android开发的过程中基本上都使用。需要一些后台的任务处理,都是通过服务实现的。比如,通过在service中与服务器保持心跳连接。
今天将service的一些知识点总结一下,分一下几个方面。
1) service的创建方式。
2) service和thread之间的关系。
3) 前台service。
一、service创建方式。
service创建方式分为两种,(startService/stopService)和(bindService/unbindService)。
a. startService/stopService
通过这种方式启动service以后,调用者和服务之间就没关系了。所有通过这种方式启动的话,不能实现service和调用者之间的通信。
startService 会经过onCreate onstart 方法,多次startService只会执行onstart方法。
b. bindService/unbindService
通过这种方式就能实现Activity和service之间的通信。当activity调用bindService时,就能获取到IBander对象,在service里边实现IBander借口,
在activity拿到IBander对象以后,就能调用service里边的方法了。通过这种方式,启动service时,service和activity是一种"不求同生,但求同死"的关系。
MyService代码
public class MyService extends Service {
private MyBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("oncreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("onDestroy");
super.onDestroy();
}
public class MyBinder extends Binder{
public void startDownload(){
System.out.println("start download");
}
}
在MyService里边添加了内部类MyBinder,继承Binder,是因为Binder实现IBinder接口。并且在方法onbind()中返回MyBinder对象。
创建ServiceConnection对象代码
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder binder = (MyBinder) service;
binder.startDownload();
}
};
bindService代码逻辑
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, conn, BIND_AUTO_CREATE); // 当服务不在时,自动创建
就能拿到MyBinder对象,因此我们就能调用服务里边的方法了,从而实现Activity和Service之间的通信。
通过bindService创建的服务,需要通过unbindService解除绑定,再进行销毁。
c. 两者之间的关系
通过startService以后,说明服务已经处于开启状态,然后再进行bindService的话,说明调用者和service之间已经产生关联。如果想销毁Service的话,单独执行stopService
或者unbindService是不够的。需要先调用stopService,将服务置为停止状态,再调用unbindService解除关系,这时,服务才能销毁。
二、service和thread之间的关系
我们常说在service里边处理后台任务,再线程里边处理一些耗时操作,防止界面出现ANR异常。那是不是意味着在线程里边干的事,都可以放在service中呢。
其实不是,它们两者之间就没什么关系,再说service运行在主线程,如果运行一些耗时逻辑,同样出现ANR异常。OK。那我就解释一下"在service里边做后台处理逻辑"
含义吧。
1. service跟acitivty没有关系,就算一个程序,所有的Activity都关闭了,只要进程不退出,service依然在运行。我们说的后台是指,所有的activity都关了,程序也关了,但是进程还在,service就还在。所以,我们一些需要长期运行的逻辑就放在service中。
2. 既然service运行在主线程中,耗时操作还得创建子线程去处理。那为啥还得使用service呢。还不如直接在activity中创建线程去处理不就结了。其实不然。
如果在activity中使用子线程去处理一些逻辑的话,当activity被回收,线程也会被回收,另外在其他的activity中很难使用在你的activity中线程处理逻辑,还有再次使用
activity中的线程处理逻辑时,很难恢复到上一次的状态。但如果使用service就简单多了,只要服务启动,所以activity绑定到service获得的是同一个IBander对象,就
算某一个activity被回收,也不会影响其他的activity。使用service能够带来很多的便利。
三、前台service
将service设置成前台service,主要有两个方面的好处。
1. 设置成前台service,不容易被系统回收。
2. 能够在系统通知栏里边显示一些信息。比如360、墨迹天气。
设置的方法为:
public void onCreate() {
<span style="white-space:pre"> </span>System.out.println("oncreate");
super.onCreate();
Notification notification = new Notification(R.drawable.ic_launcher,
"通知到来", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, "通知标题", "这是通知内容",
pendingIntent);
startForeground(1, notification);
}
设置以后,界面如图:
只要服务启动,在通知栏里边就能一直显示。