1、生命周期上的区别
执行startService时,Service会经历onCreate->onStartCommand。当执行stopService时,直接调用onDestroy方法。调用者如果没有stopService,Service会一直在后台运行,下次调用者再起来仍然可以stopService。
执行bindService时,Service会经历onCreate->onBind。这个时候调用者和Service绑定在一起。调用者调用unbindService方法或者调用者Context不存在了(如Activity被finish了),Service就会调用onUnbind->onDestroy。这里所谓的绑定在一起就是说两者共存亡了。
多次调用startService,该Service只能被创建一次,即该Service的onCreate方法只会被调用一次。但是每次调用startService,onStartCommand方法都会被调用。Service的onStart方法在API 5时被废弃,替代它的是onStartCommand方法。
第一次执行bindService时,onCreate和onBind方法会被调用,但是多次执行bindService时,onCreate和onBind方法并不会被多次调用,即并不会多次创建服务和绑定服务。
2、调用者如何获取绑定后的Service的方法
onBind回调方法将返回给客户端一个IBinder接口实例,IBinder允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。我们需要IBinder对象返回具体的Service对象才能操作,所以说具体的Service对象必须首先实现Binder对象。
3、既使用startService又使用bindService的情况
如果一个Service又被启动又被绑定,则该Service会一直在后台运行。首先不管如何调用,onCreate始终只会调用一次。对应startService调用多少次,Service的onStart方法便会调用多少次。Service的终止,需要unbindService和stopService同时调用才行。不管startService与bindService的调用顺序,如果先调用unbindService,此时服务不会自动终止,再调用stopService之后,服务才会终止;如果先调用stopService,此时服务也不会终止,而再调用unbindService或者之前调用bindService的Context不存在了(如Activity被finish的时候)之后,服务才会自动停止。
那么,什么情况下既使用startService,又使用bindService呢?
如果你只是想要启动一个后台服务长期进行某项任务,那么使用startService便可以了。如果你还想要与正在运行的Service取得联系,那么有两种方法:一种是使用broadcast,另一种是使用bindService。前者的缺点是如果交流较为频繁,容易造成性能上的问题,而后者则没有这些问题。因此,这种情况就需要startService和bindService一起使用了。
package com.zhangyu.servicetest;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
}
private int anInt;
@Override
public void onCreate() {
super.onCreate();
simpleBinder = new SimpleBinder();
Log.e(TAG, "service onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return simpleBinder;
}
public static void start(Context context) {
Intent starter = new Intent(context, MyIntentService.class);
context.startActivity(starter);
}
@Override
protected void onHandleIntent(Intent intent) {
for (int i = 0; i < 10; i++) {
anInt = i;
try {
Thread.sleep(1000 * 1);
if (onCallBackListener != null) {
onCallBackListener.callBack(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e(TAG, "onHandleIntent: " + i);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "service onDestroy");
}
private SimpleBinder simpleBinder;
public class SimpleBinder extends Binder {
public int getInt() {
return anInt;
}
public void setOnCallBackListener(OnCallBackListener callBackListener){
onCallBackListener = callBackListener;
}
}
private OnCallBackListener onCallBackListener;
public interface OnCallBackListener{
void callBack(int i);
}
}
package com.zhangyu.servicetest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
private MyIntentService myIntentService;
private Intent startIntent;
private Intent bindIntent;
public static void start(Context context) {
Intent starter = new Intent(context, Main2Activity.class);
context.startActivity(starter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_start_service:
startService();
break;
case R.id.bt_stop_service:
stopService();
break;
case R.id.bt_bind_service:
bingServide();
break;
case R.id.bt_unbind_service:
unbingServide();
break;
}
}
// 创建一个 ServiceConnection 对象
final ServiceConnection connection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder service) {
MyIntentService.SimpleBinder binder = (MyIntentService.SimpleBinder) service;
int anInt = binder.getInt();
Log.e(TAG, "onServiceConnected: anInt=" + anInt);
binder.setOnCallBackListener(new MyIntentService.OnCallBackListener() {
@Override
public void callBack(int i) {
Log.e(TAG, "callBack: " + i);
}
});
}
};
/**
* 启动一个服务
*/
private void startService() {
if (startIntent == null) {
startIntent = new Intent(this, MyIntentService.class);
}
startService(startIntent);
}
/**
* 停止一个服务
*/
private void stopService() {
if (startIntent != null) {
stopService(startIntent);
}
}
/**
* 绑定一个服务,后在启动它,这样可以同时具有绑定和启动的两种特性。
*/
private void bingServide() {
if (bindIntent == null) {
bindIntent = new Intent(this, MyIntentService.class);
}
bindService(bindIntent, connection, BIND_AUTO_CREATE);
startService(bindIntent);
}
/**
* 解绑一个服务
*/
private void unbingServide() {
if (bindIntent != null) {
unbindService(connection);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbingServide();
}
}
完!!!