一, Service 简介
Service 是 Android 四大组件之一,可与其它组件进行交互,一般运行于后台,如 IM 软件, 音乐播放器等。 这里对 Service 的常用属性进行一次简单的解析。
这里对 Service 的使用做一个初步的记录。
二, Service 生命周期
首先,我们来看一下 Google 官方给定的一个 Service 生命周期图
Service 一般有两种方式进行启动, context.startService(), context.bindService(). 而对应的销毁方式则分别是 context.stopService, context.unbindService.
来看代码,首先定义 四个按钮分别代表上面四个操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/start_service"
android:text="start service"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/stop_service"
android:text="stop service"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bind_service"
android:text="bind service"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/unbind_service"
android:text="unbind service"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
定义我们的 Service:
public class MyService extends Service{
@Override
public void onCreate() {
super.onCreate();
log("onCreate");
}
@Override
public boolean onUnbind(Intent intent) {
log("onUnbind");
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
log("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
log("onDestroy");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
log("onBind");
return null;
}
private static void log(String msg){
Log.e("MyService", msg);
}
}
也别忘了在 Manifest 文件中声明:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.leo.myservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
在 MainActivity 中处理事件
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "MainActivity";
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "Service Connected, name: " + name + ", binder: " + service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "Service disconnected, name: " + name);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start_service).setOnClickListener(this);
findViewById(R.id.stop_service).setOnClickListener(this);
findViewById(R.id.bind_service).setOnClickListener(this);
findViewById(R.id.unbind_service).setOnClickListener(this);
}
private void startService(){
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
private void stopService(){
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
private void bindService(){
bindService(new Intent(this, MyService.class), connection, Service.BIND_AUTO_CREATE);
}
private void unbindService(){
unbindService(connection);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:startService();break;
case R.id.stop_service:stopService();break;
case R.id.bind_service:bindService();break;
case R.id.unbind_service:unbindService();break;
}
}
}
点击 start service 后
然后点击 stop service
点击 bind service 后再点击 unbind service:
这里可以发现并没有运行 MainActivity 中 ServiceConnection 中的回调。原因在于当前Service 并没有对应的 binder 来,也即没有真正连接上,所以 onConnecton 的回调不会进行,下面看 MyService 和 MyBinder 进行连接。
这里我们在 MyService 中定义一个 MyBinder
public static class MyBinder extends Binder{}
修改 onBind 方法
@Nullable
@Override
public IBinder onBind(Intent intent) {
log("onBind");
return new MyBinder();
}
然后点击 bind service, unbind service
可以看到, service 在 onBind 的时候返回的不是 null 时, connection 就建立了连接了。有人或许会奇怪为什么 onServiceDisconnected并没有运行,实际上该回调发生在非正常失去连接的情况,如内存不足杀死 Service 的情况,在 Service 重新运行后 会重新进入 connected 回调。
三, Activity 和 Service 一般连接
我们可以在 MyBinder 中定义 public 方法直接让 Service 进行工作,那么接下来我们改动一下 MyBinder, 在中间添加一个下载任务:
public static class MyBinder extends Binder{
public void startDownload(){
log("start download");
try{
Thread.sleep(30 * 1000);
}catch (InterruptedException e){
Log.e("MyService", "Thread Crashed", e);
}
log("download finished");
}
}
再来点击 bind service:
嗯,开始下载,正确,嗯?不对!为什么会 ANR:
原来, service 相关的这些函数都是运行在 UI 线程的,来看看:
首先我们在 MainActivity 中增加一条 log:
private void bindService(){
Log.e("MainActivity", "bindService");
bindService(new Intent(this, MyService.class), connection, Service.BIND_AUTO_CREATE);
}
然后重复上面的操作:
我们可以看到 PID-TID 一列, Service 和 MainActivity 的值是一样的,这说明他们在同一个进程,同一个线程,也即 UI 线程,在 UI 线程做耗时操作不给你 ANR 给谁 ANR 呢?
以上为一般连接的情况,更多连接状态待续。
关于 Service 的更多使用,这里有一个简单的 DEMO
Android Service 使用,一个简单音乐播放器 DEMO(二)
四,一些需要注意的点
1. 多次 bind service 只会运行一次,因为 service 已经 bind 了
2. 一个 service 只有一个实例,因此在 onCreate 之后如果没有 onDestroy 那么就不会再次 onCreate;
3. 多次点击 start service 会多次调用 onStartCommand.
4. 销毁 service 时可以在service 内部调用 stopSelf
5. 对同一个 service 调用了 startService 和 bindService, 在 service 外需要 stopService 和 unbindService 都调用才能 destroy
五,参考博客
1. 郭霖大神的博客