Service本质上是后台运行的可执行程序,它没有Activity那样的用户界面
Service不能与用户进行交互,不能自已启动,运行在后台
启动Service
有两种方法:
1): 通过Context.startService()方法启动一个指定的Service
startService(Intent);
Intent intent = new Intent(起始组件对象,目标Service.class);
startService(intent);
Service不能与用户进行交互,不能自已启动,运行在后台
启动Service
有两种方法:
1): 通过Context.startService()方法启动一个指定的Service
startService(Intent);
Intent intent = new Intent(起始组件对象,目标Service.class);
startService(intent);
启动后的Service将在后台运行,具有自已的生命周期与当前组件不再有直接关联
eg:
startService(new Intent(MainActivity.this,MyService.class));
public class MyService extends Service {
private String TAG = "MyService";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
//不涉及绑定服务,返回null
return null;
}
//Service类创建时触发本方法
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG,"Service is onCreate ");
}
//Service类的实例被启动时触发
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
//Service的一个实例被收回时,执行此方法
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"Service onDestory");
}
}
当点击启动服务按钮时:
I/MyService: Service is onCreate
I/MyService: Service onStartCommand
点击关闭服务按钮时:
I/MyService: Service onDestory
2)通过Context.bindService()方法将一个指定的Service与当前的组件(如Activity)绑定
bindService()方法在绑定时,可以设置启动并绑定,或只绑定不启动
bindService(Intent intent, ServiceConnection conn, int flags);
作用:与intent指定的目标Service绑定
参数1:启动intent对象中指定的目标Service
参数2:是一个ServiceConnection对象,该对象用于监听访问者与Service之间的连接情况
当绑定成功时,调用ServiceConnection对象的onServiceConnected方法当解除绑定时调用onServiceDisconnected方法
参数3:指定绑定时是否自动创建 Service,0:不自动创建Service,BIND_AUTO_CREATE:自动创建Service
onServiceConnected(ComponentName name, IBinder iBinder)
作用:绑定成功时,触发本方法
参数1:被绑定的Service的一个实例
参数2:通过被绑定的Service类中的onBind方法返回一个IBind类型的对象
eg:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btbind;
private Button btunbind;
MyService.MyBinder mBinder;//定义成员变量
private String TAG = "MyService";
private boolean mIsBind = false;
private ServiceConnection mConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//绑定成功时触发
mBinder = (MyService.MyBinder)iBinder;
mBinder.print("Hello this is from MainActivity ---");
Log.i(TAG,"onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(TAG,"onServiceDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btbind = (Button)findViewById(R.id.button);
btunbind = (Button)findViewById(R.id.button2);
btbind.setOnClickListener(this);
btunbind.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
bindService(new Intent(MainActivity.this,MyService.class),
mConn, BIND_AUTO_CREATE);
mIsBind = true;
break;
case R.id.button2:
if(mIsBind){
unbindService(mConn);
mIsBind = false;
}
break;
}
}
}
public class MyService extends Service {
private String TAG = "MyService";
public MyService() {
}
MyBinder mBinder;
//自定义内部类,该类是Binder的子类,用于绑定时传递对象
public class MyBinder extends Binder{
public void print(String text){
Log.i(TAG,text);
}
}
//将对象返回至绑定的Activity
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,"MyService onBind()");
return mBinder;
}
//Service类创建时触发本方法
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG,"Service is onCreate ");
mBinder = new MyBinder();
}
//Service类的实例被启动时触发
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
//解除绑定时调用
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,"MyService onUnbind");
return super.onUnbind(intent);
}
//再次绑定时调用
@Override
public void onRebind(Intent intent) {
Log.i(TAG,"MyService onRebind()");
super.onRebind(intent);
}
}
当点击绑定服务时
I/MyService: Service is onCreate
I/MyService: MyService onBind()
I/MyService: Hello this is from MainActivity ---
I/MyService: onServiceConnected
当点击解除绑定时
I/MyService: MyService onUnbind
Service生命周期图