随着应用程序启动Service方式的不同,Service对的生命周期也略有差异。
- 如果应用程序通过startService()方法来启动Service,Service的生命周期如图左边所示
- 如果应用程序通过bindService()方法来启动Service,Service的生命周期如图右边所示
Service生命周期还有一种特殊的情形,如果Service已由某个客户端通过startService()方法启动了,接下来其他客户端在调用bindService()方法来绑定该Service后,再调用unbindService()方法来解除绑定,最后又调用了bindService()方法再次绑定到Service,这个过程所触发的生命周期测试结果如下:
07-03 14:59:24.760 32244-32244/com.mystudy.kibi.networktype E/==>: onCreate
07-03 14:59:24.761 32244-32244/com.mystudy.kibi.networktype E/==>: onStartCommand
07-03 14:59:27.141 32244-32244/com.mystudy.kibi.networktype E/==>: onBind
07-03 14:59:28.899 32244-32244/com.mystudy.kibi.networktype E/==>: onUnbind
07-03 14:59:30.556 32244-32244/com.mystudy.kibi.networktype E/==>: onRebind
07-03 14:59:34.077 32244-32244/com.mystudy.kibi.networktype E/==>: onUnbind
07-03 14:59:39.455 32244-32244/com.mystudy.kibi.networktype E/==>: onRebind
07-03 14:59:49.085 32244-32244/com.mystudy.kibi.networktype E/==>: onUnbind
07-03 14:59:49.086 32244-32244/com.mystudy.kibi.networktype E/==>: onDestroy
onCreate()→onStartCommand()→onBind()→onUnbind()[重写该方法时返回了true]→onRebind()
注意:如果希望onRebind()方法被回调时,除了需要该Service是由Activity的startService()方法启动的之外,还需要Service子类重写onUnbind()方法。
Service子类:
package com.mystudy.kibi.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* Created by KIBI熊 on 16/6/28.
*/
public class TestService extends Service{
private MyBinder myBinder = new MyBinder();
private boolean run;
private int count;
@Override
public IBinder onBind(Intent intent) {
Log.e("==>","onBind");
return myBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("==>","onCreate");
run = true;
count = 0;
new Thread(){
@Override
public void run() {
while (run){
try {
sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("==>","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("==>","onUnbind");
super.onUnbind(intent);
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.e("==>","onRebind");
}
@Override
public void onDestroy() {
Log.e("==>","onDestroy");
super.onDestroy();
run = false;
}
public class MyBinder extends Binder{
public int getCount(){
return count;
}
}
}
MainActivity:
package com.mystudy.kibi.networktype;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.mystudy.kibi.service.TestService;
public class MainActivity extends AppCompatActivity {
private Button bindBtn,unBindBtn,startBtn,stopBtn;
private TestService.MyBinder binder;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (TestService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("==>","onServiceDisconnected");
}
};
private final Intent INTENT = new Intent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
initListener();
}
private void initView(){
bindBtn = (Button) findViewById(R.id.bind_btn);
unBindBtn = (Button) findViewById(R.id.unbind_btn);
startBtn = (Button) findViewById(R.id.start_btn);
stopBtn = (Button) findViewById(R.id.stop_btn);
}
private void initListener(){
bindBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(INTENT,serviceConnection, Service.BIND_AUTO_CREATE);
}
});
unBindBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(serviceConnection);
}
});
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(INTENT);
}
});
stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(INTENT);
}
});
}
private void initData(){
INTENT.setAction("com.mystudy.kibi.service.TEST_SERVICE");
INTENT.setPackage(getPackageName());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mystudy.kibi.networktype">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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="com.mystudy.kibi.service.TestService">
<intent-filter>
<action android:name="com.mystudy.kibi.service.TEST_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context="com.mystudy.kibi.networktype.MainActivity">
<Button
android:layout_marginTop="50dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="bind"
android:textColor="#888"
android:id="@+id/bind_btn" />
<Button
android:layout_marginTop="50dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="unbind"
android:textColor="#888"
android:id="@+id/unbind_btn" />
<Button
android:layout_marginTop="50dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="start"
android:textColor="#888"
android:id="@+id/start_btn" />
<Button
android:layout_marginTop="50dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="stop"
android:textColor="#888"
android:id="@+id/stop_btn" />
</LinearLayout>
在测试过程中,点击解除绑定按钮时会发现,Service没有回调onDestroy()方法,这是因为该Service并不是由Activity通过bindService()方法来启动的(该Service是先由Activity通过startService()方法来启动的),因此当Activity调用unBindService()方法取消与该Service的绑定时,该Service也不会终止。
由此可见,当Activity调用bindService()绑定一个已启动的Service时,系统只是把Service内部IBinder对象传给Activity,并不会把该Service生命周期完全“绑定“到该Activity,因为当Activity调用unBindService()方法取消与该Service的绑定时,也是值切断该Activity与Service之间的关联,并不会停止该Service组件。