前言
Service可以理解为没有布局的Activity,可以进行音乐播放,后台下载等操作。
注意:Service是运行于主线程中的,不能进行耗时操作。
如何创建一个Service
Service从创建到启动涉及到新进程创建和跨进程通信。
Service的启动流程:
- Process A进程采用Binder IPC向system_server进程发起startService请求;
- system_server进程接收到请求后,向zygote进程发送创建进程的请求;
- zygote进程fork出新的子进程Remote Service进程;
- Remote Service进程,通过Binder IPC向sytem_server进程发起attachApplication请求;
- system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向remote Service进程发送scheduleCreateService请求;
- Remote Service进程的binder线程在收到请求后,通过handler向主线程发送CREATE_SERVICE消息;
- 主线程在收到Message后,通过发射机制创建目标Service,并回调Service.onCreate()方法。
到此,Service便正式启动完成。当创建的是本地服务或者服务所属进程已创建时,则无需经过上述步骤2、3,直接创建服务即可。
Service启动方式1
利用Intent意图,调用startService方法。
被启动后会立马执行onCreate和onSatartCommand方法,Activity退出之后,不会调用onDestroy方法销毁Service。
Intent intent = new Intent(this, MyService.class);
startService(intent);
Service启动方式2
调用bindService,Activity可以与Service进行通信。如果用绑定方式来启动服务,调用bindService方法时需要传递三个参数,分别是Intent, ServiceConnection, int flags
Intent intent = new Intent(this, MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
第一个参数,intent意图里设置要启动的Service;
第二个参数,ServiceConnection要new出来,是一个接口实现,需要重写它的两个方法:onServiceConnected, onServiceDisconnected。
可以看到第一个回调方法onServiceConnected()中需要一个IBinder类型的对象,而Service的onBond()方法所返回的就是一个IBinder类型。
可以在Service下创建一个MyBinder类,来进行双向的通信,然后在onBind()方法里返回一个MyBinder对象。
然后在Activity中,创建ServiceConnection时,在onServiceConnected()方法里使用这个MyBinder类(可能需要类型转换),可以直接调用它的方法。
还能在MyBinder里创建一个MyService对象,构造方法里将MyService对象传进去,就可以在MyBinder里使用Service的方法,进而Activity也能使用MyService的方法了。
bindService()的调用顺序,首先是Service里的onBind()方法, 然后返回一个MyBinder对象到onServiceConnected(),再执行这个方法。利用返回的MyBinder对象,就可以调用Service里的方法了。
第三个参数,一般填BIND_AUTO_CREATE,自动创建。
注意,使用这种启动Service的方法,当activity退出之后,会执行onUnbind()方法,和onDestroy()方法。
Service的生命周期
startService方法启动的Service
在创建阶段调用onCreate(),后面在每次statrService()时调用一次onStartCommand(),销毁时由Activity直接调用stopService(),或者利用startService()的intent里传递“stop”信息,给Service进行判断,信息比对一致,Service再调用stopSelf()来停止服务。
同样的,也可以在intent里放一些其他的信息,通过startService发送给Service,再在Service的onStartCommand方法里进行判断,从而执行不同的操作。Service与Activity的通信也可以通过回调来完成。
最后阶段即,Service自己调用其onDestroy()方法。
bindService方法启动的生命周期
onCreate()方法和onBind()方法在绑定的时候调用一次。
一个Service可以有多个Activity跟其进行绑定,在所有的Activity都和它解绑的时候,这个Service才会调用onUnbind()和onDestroy()。
通过广播实现二者通信
public class MainActivity extends Activity {
private ProgressBar mProgressBar;
private Intent mIntent;
private MsgReceiver msgReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册广播接收器
msgReceiver = new MsgReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.communication.RECEIVER");
registerReceiver(msgReceiver, intentFilter);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//启动服务
mIntent = new Intent("com.example.communication.MSG_ACTION");
startService(mIntent);
}
});
}
@Override
protected void onDestroy() {
//停止服务
stopService(mIntent);
//注销广播
unregisterReceiver(msgReceiver);
super.onDestroy();
}
/**
* 广播接收器
* @author len
*
*/
public class MsgReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//拿到进度,更新UI
int progress = intent.getIntExtra("progress", 0);
mProgressBar.setProgress(progress);
}
}
}
Service代码
public class MsgService extends Service {
/**
* 进度条的最大值
*/
public static final int MAX_PROGRESS = 100;
/**
* 进度条的进度值
*/
private int progress = 0;
private Intent intent = new Intent("com.example.communication.RECEIVER");
/**
* 模拟下载任务,每秒钟更新一次
*/
public void startDownLoad(){
new Thread(new Runnable() {
@Override
public void run() {
while(progress < MAX_PROGRESS){
progress += 5;
//发送Action为com.example.communication.RECEIVER的广播
intent.putExtra("progress", progress);
sendBroadcast(intent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startDownLoad();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
前台Service
- 前台服务可以说是除了绑定式Service和非绑定式Service之外,又一种Service类型。
- 顾名思义,它是运行在前台可以和用户打交道的Service。
- 优先级相比另外两个运行在后台的Service要高,几乎不会被系统回收。
使用场景
前台服务必须显示通知(Notification)。因此,前台服务是以通知的形式呈现的。而且该通知是不可去除的,除非服务停止或者从前台移除。