Service介绍
Service是Android四大组件之一,是一个后台运行的组件,执行长时间运行且不需要用户交互的任务。即使应用被销毁也依然可以工作。
生命周期
image.png上图展示了Service的生命周期过程,Service有两种启动方式,对应了两种不同的生命周期展现。
-
通过startService方式启动(左图)
生命周期:onCreate—onStartCommand—onDestroy
⚠️多次调用startService方法,onCreate方法只会调用一次,而onStartCommand方法调用多次。
停止Service调用stopService方法 -
通过bindService方式启动(右图)
生命周期:onCreate—onBind—onUnBind—onDestroy
⚠️多次调用onBind方法,onCreate方法只会调用一次,而onBind方法调用多次。
停止Service先调用unBind方法,再调用stopService方法 -
onStartService和onBind同时调用
1、先调用startService再调用onBind,
onCreate() --onStartCommand()--onBind--onUnBind--onDestroy
2、先调用onBind再调用startService
onCreate() --onBind()--onStartCommand--onUnBind--onDestroy
Service的创建和使用
创建Service
public class MyService extends Service {
@Override
public ComponentName startService(Intent service) {
Log.d("MyService", "startService()");
return super.startService(service);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("MyService", "onBind()");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate()");
mBinder = new MyBinder();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("MyService", "onUnbind()");
return super.onUnbind(intent);
}
public class MyBinder extends Binder {
public void onDone() {
Log.d("MyService", "onDone()");
}
}
- startService方式
//MainActivity.java
textView.setOnClickListener(v -> startServices());
Intent serviceIntent;
private void startServices() {
serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
在Activity销毁时,销毁服务
@Override
protected void onDestroy() {
super.onDestroy();
stopService(serviceIntent);
}
- onBind方式
//MainActivity.java
textView.setOnClickListener(v -> startServices());
private MyService.MyBinder myBinder;
//创建服务连接
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
Log.d("MainActivity", "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
private void startServices() {
Intent serviceIntent = new Intent(this, MyService.class);
bindService(serviceIntent, mConnection, BIND_AUTO_CREATE);
}
onBind的方式,需要创建一个ServiceConnection,用来和服务进行连接。
在Activity销毁时,断开链接。
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mConnection);
}
⚠️别忘了在androidManifest.xml注册Service
<service android:name=".MyService"/>
Service和Activity的交互
- Activity调用Service方法
1、通过Service中Binder来调用内部方法
public class MyService extends Service {
MyBinder mBinder;
@Override
public IBinder onBind(Intent intent) {
Log.d("MyService", "onBind()");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate()");
mBinder = new MyBinder();
}
//服务中定义的方法
public void onServiceDone(String param){
Log.d("MyService", "service execute param = " + param);
}
public class MyBinder extends Binder {
public void onDone(String param) {
onServiceDone(param);
}
}
//MainActivity.java
//通过从Service中获取的Binder来执行方法
textView.setOnClickListener(v -> myBinder.onDone("Activity param"));
private MyService.MyBinder myBinder;
//创建服务连接
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//拿到Service中的Binder对象
myBinder = (MyService.MyBinder) service;
}
};
2、或者是通过Service中的binder获取到Service对象本身
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
textView.setOnClickListener(v ->
//通过binder获取到Service对象 然后直接调用方法
myBinder.getService(). onServiceDone("Activity param")
);
private MyService.MyBinder myBinder;
//创建服务连接
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//拿到Service中的Binder对象
myBinder = (MyService.MyBinder) service;
}
};
- Service调用Activity的方法
可以通过广播,Activity注册相应的接收器来实现。
网友评论