一、Service是android的四大组件,于activity最相似,于activity的区别在于,service一直运行在后台,它没有界面,所以绝不可能跑到前台,一旦service被启动起来,它就有它自己的额生命周期。service是四大组件,也是需要到androidManifest.xml文件中配置声明。
二、开发service的两个步骤。
1、定义一个继承service的子类。
2、在androidManifest.xml文件中配置声明。
(service和ctivity还有一袋奶相识的地方就是,他们都是从context派生出来的,因此都可以调用Context中的getResources(),getContentResoler()等方法)
三、Service也定义了一系列的生命周期。
1、IBinder onBind(Intent intent):该方法是service子类必须要实现的方法,该方法返回一个IBinder对象,应用程序就可以通过IBinder对象,调用IBinder类中的方法,实现activity于service的通信。
2、void onCreate():该service第一次被创建后就立即回调该方法。
3、void onDestroy():该方法在service关闭之前调用。
4、void onStartCommand(Intent intent,int flags,int startId):该方方法每次客户端调用startService(Intent)方法启动该方法。
5、boolean onUnbind(Intent intent):方法在service上绑定所有客户端都断开时就将回调该方法。
四、在android运行service有两个方式:
1、通过context的startService()方法,该方法启动了service,访问者就和service之间就没有任何关联了,即使访问者推推出,service也运行。
(其中onCreate()方法之调用一次,其它时候启动就调用onStartCommand();)
(停止service的方法就是调用stopService()方法)
例、
Intent intent=new Intent(this,FristService.class);
startService(intent);
stopService(intent);
2、通过context中的bindService()方法,调用该方法,service就启动,访问者就和service之间绑定在一起了,访问者一但推出,service也终止。
五、绑定本地Service并与之通信
要实现service和访问者之间的通信,就需要将service和访问者绑定。
1、绑定:bindService(Intent intent,ServiceConnection conn,int flags)
intent:用intent启动service
conn:该ServiceConnection对象用于监听Service和访问者之间的连接情况。
(连接成功就回调方法:onServiceConnected(ComponentName,IBinder ibinder);该方法将ibinder导出,就可实现通信)
(连接失败回调:onServiceDisconnected(ComponentName name);)
flags:为指定绑定时是否自动创建Service,若参数为0(不自动创建),若为BIND_AUTO_CREATE(自动创建);
2、解绑:unbinService(ServiceConnection conn)
例、
重写的一个service的子类:
public class MyService extends Service {
private int count=0;
private boolean quit=false;
private MyBinder binder=new MyBinder();//次对象用于方法public IBinder onBind(Intent intent)返回
public class MyBinder extends Binder{ //定义的一个MyBinder类可调用其中的方法
public int getCount(){
return count;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.print("Service is Binder");
return binder;
}
public void onCreate(){ //创建的时候开启一个线程
super.onCreate();
System.out.print("Service is Create");
new Thread(){
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
public boolean onUnbind(Intent intent){ //解帮的时候回调
System.out.print("Service is onUnbind");
return true;
}
public void onDestroy(){ //销毁的时候回调
super.onDestroy();
this.quit=true;
System.out.print("Service is onDestroy");
}
}
在activity中来使用service
public class ServiceActivity extends AppCompatActivity implements View.OnClickListener{
MyService.MyBinder myBinder;
private ServiceConnection connection=new ServiceConnection() {//用于监听连接情况
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { //连接成功
System.out.print("service Connected");
myBinder=(MyService.MyBinder)iBinder; //将使用iBinder
}
@Override
public void onServiceDisconnected(ComponentName componentName) { //连接失败
System.out.print("service Disconnected");
}
};
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.service_activity);
Button bind=findViewById(R.id.bind);
Button unbind=findViewById(R.id.unbind);
Button state=findViewById(R.id.state);
bind.setOnClickListener(this);
unbind.setOnClickListener(this);
state.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bind:{ //绑定service
Intent intent=new Intent(this,MyService1.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE); //绑定的时候自动创建service
break;
}
case R.id.unbind:{
unbindService(connection); //解绑
break;
}
case R.id.state:{
//调用服务的方法实现通信
Toast.makeText(ServiceActivity.this,"Count:"+myBinder.getCount(),Toast.LENGTH_SHORT).show();
break;
}
default:
break;
}
}
}
六、service的生命周期:
1、如果用startService启动服务
调用---->onCreate()----->onStartCommand()--->service自己运行----->onDestroy()---->service关闭
七、IntentService
对于异步的startService请求,每当客户通过Intent请求来启动该服务,Intent会将Intent加入队列中,然后开启一条新的worker线程来处理Intent,因此不会阻塞主线程。IntentService自己处理耗时的任务。
特征:
1、单独地创建worker线程来处理Intent请求;
2、单独地worker线程来处理onHandleIntent()方法实现地 ,开发者无需处理多线程问题。
3、当请求处理完成,IntentService会自动停止,开发者无需调用stopSelf()方法。
4、onBinder()方法默认实现,默认返回null
5、startCommand()地方法默认实现,实现将请求地Intent添加到队列中。
系统服务将在下一节讲到。