Android的Service一直都是蒙在心里,Service究竟是什么?其实Service和Activity是对立的东西,Activity在Android系统中最要的就是跟用户交互,但是Service是不用交互的,主要就是长期在后台运行(重点:它默认并不会运行在子线程中,也不是独立运行在独立进程中,同样还是运行在UI线程里面,同样也不能做耗时的操作)。
了解Service一定要一定要理解他们的生命周期
说到生命周期,一开始我们都不知都干嘛用,为什么要有生命周期的这个东西?设计生命周期的目的是满足了面向对象设计。
======onCreate:
=====onStartCommand:
=====onDestroy:
其实服务的生命周期很简单,只有三生命周期方法个onCreate
、onStartCommand
和onDestroy
。
启动一个服务,当我们调用
Intent intent = new Intent(mContext, MyService.class);
startService(intent);
启动一个服务的时候,会执行onCreate
、onStartCommand
这两个方法,但是我们再次调用上面的代码的时候,你会发现不会走onCreate
的方法了,只会走onStartCommand
这个方法了。细心的朋友会发现,在Android中很多情况比如创建一个数据库,都是只会走一次onCreate
。
服务启动了,那我们怎么暂停服务呢?
只需要调用在服务里面调用stopSelf()
和stopService()
这个两个方法就可以把服务给关闭了。
stopService(new Intent(mContext,MyService.class));
上面我们已经提到了,服务也是运行在UI线程里面的,所以不能执行耗时的操作,否则会出现ANR。所以服务通常有一套模板
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
LogUtil.log("======onCreate:");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtil.log("======onStartCommand:");
doSomething();
return super.onStartCommand(intent, flags, startId);
}
/**
* 由于服务不能执行耗时的操作,所以这里要启动一个行程执行耗时操作
*/
private void doSomething() {
new Thread(new ServiceThread()).start();
}
@Override
public void onDestroy() {
LogUtil.log("======onDestroy:");
super.onDestroy();
}
}
然后Google早已经了解一切了,假如你要启动一个服务就是为了做一下简单的操作,做完就立即关闭这个服务还有调用stopSelf()
和stopService()
两个方法,那不是很麻烦?其实Google已经已经提供了另一种服务IntentService。
public class MyIntentService extends IntentService {
public MyIntentService() {
// 调用父类的有参构造函数
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
// 执行耗时操作
LogUtil.log("======执行耗时操作完毕");
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.log("======服务已经停止");
}
}
是不是很简单,只要启动它,然后做耗时的操作就行了,这个服务就会自动的关闭。启动方式和startService的方式一样
然后服务并没有那么简单,你是不是觉得少了一些东西,那就是跟Service进行交互了,这不得说起服务的另一种启动方式,通过bindService的方式进行启动。
public class MyBinderService extends Service {
//
private DownloadBinder mBinder = new DownloadBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
LogUtil.log("===Binder===onBind");
return mBinder;
}
@Override
public void onCreate() {
LogUtil.log("======Binder=====onCreate");
super.onCreate();
}
@Override
public boolean onUnbind(Intent intent) {
LogUtil.log("======Binder=====onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
LogUtil.log("======Binder=====onDestroy");
super.onDestroy();
}
}
else if (mBtnStartBinder.getId() == v.getId()) {
if (myServiceConnection==null){
myServiceConnection = new MyServiceConnection();
}
Intent intent = new Intent(mContext, MyBinderService.class);
bindService(intent, myServiceConnection, BIND_AUTO_CREATE);
} else if (mBtnStopBinder.getId() == v.getId()) {
if (myServiceConnection != null) {
unbindService(myServiceConnection);
myServiceConnection = null;
}
}
bindService只会启动一次,无论你调多少次bindService,不会多次启动最后:所有的Service都需要在清单文件中注册,服务有,startService(3个生命周期方法:onCreate ==> onStartCommand ==>onDestroy),bindService(4个生命周期方法:onCreate==>onBind–onUnbind==>onDestroy),IntentService,前台服务(类似于天气预报属于startService的一种),一共四种!