1、startService( ):访问者与调用者之间没有关联,即使访问者退出,Service仍然继续运行
生命周期:onCreate()-》onStartCommand()-》onDestroy()
note:首次创建调用onCreate(),多次调用已有的Service时调用onStartCommand
2、bindService( ):访问者与调用者绑定在一起,访问者一旦退出,Service也就终止
和startService的区别:想使用service中的一些数据或者访问其中的一些方法时,就要使用bindService()
生命周期:onCreate()-》onBind()-》onUnbind()-》onDestroy()
3、用法:
(1)定义service
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.os.Binder;
import android.widget.Toast;
/**
* 类描述:自定义服务
*/
public class MyService extends Service {
private static final String TAG = "##!##";
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder {
public void test() {
Log.d(TAG, "test() called with: " + "");
Toast.makeText(getApplicationContext(), "调用了Service中的test()方法", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate() called with: " + "");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() called with: " + "intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind() called with: " + "intent = [" + intent + "]");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind() called with: " + "intent = [" + intent + "]");
return true; //此处不能用super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy() called with: " + "");
super.onDestroy();
}
}
(2)在ManiFest中注册
<service android:name=".service.MyService"/>
(3)启动
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
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 com.limonzet.googletest.R;
/**
* 类描述:Service测试
*/
public class ServiceActivity extends Activity implements View.OnClickListener {
MyService.MyBinder mBinder;
private boolean mIsBind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
findViewById(R.id.btn_start_service).setOnClickListener(this);
findViewById(R.id.btn_stop_service).setOnClickListener(this);
findViewById(R.id.btn_bind_service).setOnClickListener(this);
findViewById(R.id.btn_unbind_service).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(ServiceActivity.this, MyService.class); //5.0之后必须显式启动
switch (v.getId()) {
case R.id.btn_start_service:
startService(intent); // startService
break;
case R.id.btn_stop_service:
stopService(intent);
break;
case R.id.btn_bind_service:
getApplicationContext().bindService(intent, conn, Service.BIND_AUTO_CREATE);
mIsBind = true;
break;
case R.id.btn_unbind_service:
if (mIsBind) { //不做判断的话,unBind一个已经解绑的Service,会报错
getApplicationContext().unbindService(conn);
mIsBind = false;
}
break;
}
}
private String TAG = "##!##";
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected() called with: " + "name = [" + name + "], service = [" + service + "]");
mBinder = (MyService.MyBinder) service;
mBinder.test();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected() called with: " + "name = [" + name + "]");
}
};
}
(4)注意点: 不要在Service中直接处理耗时任务,可以开子线程处理