安卓开发四大组件之一——Service,可以理解为幕后工作者,音乐词曲作家,哈哈。那么在开发中,音乐播放,下载文件,上载文件都用到service。
Service,分为本地服务和远程服务,区分是客户端和服务端是否在同一进程当中,在为本地服务,不在为远程服务,下面着重介绍本地服务,是安卓开发中最常用到的。
启动服务有两种方式,startService和bindService;
结束服务有两种方式,startService对应stopService或stopSelf,bindService对应unBindService;
startService和bindService;——两者之间的区别:startService启动服务后,客户端和服务端再就没有联系了,也就是之间没有通信,要结束服务,要不在启动服务的地方调用stopService或者在服务中调用stopSelf;bindService启动服务后,服务和客户端是有交互的,他们之间是通过binder对象进行交互的,要结束服务,在客户端调用unBindService,bindService在开发中是最常用的,所以着重要讲解。
切记在使用service时,一定要在AndroidManifest.xml中进行配置,如何配置,Activity怎么配置,服务就怎么配置。例如项目中下载视频服务的配置
<!-- 下载视频服务器 -->
<service android:name="com.suowei.appsuowei.service.UpVideoService"/>
1、startService:
生命周期:
onCreate ---> onStart ---> onDestory(Android 2.0以下版本中使用)
onCreate ---> onStartCommand ---> onDestory(Android2.0以上版本中使用)
当第一次调用startService后,先调用onCreate,再调用onStart或者onStartCommand,结束服务,调用onDestory,这里要特别注意的,如果已经启动了该服务,如果再次调用startService,那么不会调用onCreate,直接调用onStart或onStartCommand。
特别注意的是:onStartCommand方法返回一个int值,这个值有四种:
this.bindService(intent, conn, Context.BIND_AUTO_CREATE);
第一个参数intent为意图,用的滚瓜烂熟了吧Intent intent=new Intent(this,UpVideoService.class);
第二个参数conn为实现了服务链接接口(ServiceConnection)的类的对象private class UpVideoConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {}
}
第一个方法在启动服务后链接上服务调用,第二个为断开服务链接时调用,大家注意,在第一个方法中传入了一个Ibinder接口变量,这个变量就是客户端与服务进行通信的桥梁纽带,客户端与服务端要通信,必须要拿到Service对象,才能调用Service中的公共方法,在android中的Service使用onBind的方法绑定服务时,返回一个实现IBinder接口的对象,所以在Service对象内部有一个Binder对象并返回,在这个对象中,写一个方法得到该Service,这样在上面的onServiceConnected中返回一个binder对象,用这个对象拿到Service对象,这样就可以随意调用Servcie中的公共方法了,实现了客户端有服务器端的通信 @Override
public IBinder onBind(Intent intent) {
return new VideoBinder;
}
上面这个方法是用来返回Binder对象的
public class VideoBinder extends Binder{
public UpVideoService getService(){
return UpVideoService.this;
}
}
那么现在就把这个类的对象进行返回,把null改为VideoBinder对象,public IBinder onBind(Intent intent) {
return new VideoBinder();}
那么这样,在客户端返回的Binder对象,调用自己的方法,就获取到了Service,接下来想怎么操作Service中的方法就看你的需要了。protected abstract void onHandleIntent(Intent intent);
这是一个抽象方法,要实现必须要重写了,通过查阅源代码,IntentService是通过Handler looper message的方式实现了一个多线程的操作,同时耗时操作也可以被这个线程管理和执行,同时不会产生ANR的情况。说来说去,大家记住喽,一代比一代永远强。private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public IntentService(String name) {
super();
mName = name;
}
/**
* Sets intent redelivery preferences. Usually called from the constructor
* with your preferred semantics.
*
* <p>If enabled is true,
* {@link #onStartCommand(Intent, int, int)} will return
* {@link Service#START_REDELIVER_INTENT}, so if this process dies before
* {@link #onHandleIntent(Intent)} returns, the process will be restarted
* and the intent redelivered. If multiple Intents have been sent, only
* the most recent one is guaranteed to be redelivered.
*
* <p>If enabled is false (the default),
* {@link #onStartCommand(Intent, int, int)} will return
* {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
* dies along with it.
*/
public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}
@Override
public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock.
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}