Service可以和Activity绑定,也可以和service绑定,service有两种绑定方法。这里拿Activityservice绑定为例。

1.第一种方法

1)activity

Intentintent=newIntent(MainActivity.this,BinderServiceTest.class);

bindService(intent,mConnection,Context.BIND_AUTO_CREATE);

//bindService最后一个参数为Context.BIND_AUTO_CREATE时会和service建立连接,会在连接建立之后调用onServiceConnected方法,bindService最后一个参数为Context.BIND_DEBUG_UNBIND时会断开和service的链接,会在断开连接后调用onServiceDisconnected()方法。

2)activity中实现个内部类,并override其方法

privateServiceConnectionmConnection=newServiceConnection(){

@Override

publicvoidonServiceConnected(ComponentNamename,IBinderservice){

//TODOAuto-generatedmethodstub

TestBinderBindService=(TestBinder)service;

Stringdata=BindService.getData();

Log.d("testBinder",data);

}

@Override

publicvoidonServiceDisconnected(ComponentNamename){

//TODOAuto-generatedmethodstub

Log.d("testBinder","Disconnected");

}

};

3)service

publicclassBinderServiceTestextendsService{

@Override

publicIBinderonBind(Intentarg0){

//TODOAuto-generatedmethodstub

IBinderbinder=newTestBinder();

returnbinder;

}

classTestBinderextendsBinder{

publicStringgetData(){

return"TestBinder";

}

}

}

2.第二种方法

1)activity

Intentintent=newIntent();

intent.setClass(TestActivity.this,FirstService.class);

startService(intent);

//startService(intent);方法时会调service中的onStartCommand()方法,当调stopService()方法时会调service中的onDestroy()方法。

2)service

publicclassFirstServiceextendsService{

@Override

publicIBinderonBind(Intentintent){

//TODOAuto-generatedmethodstub

System.out.println("ServiceonBind");

returnnull;

}

//当创建一个Servcie对象之后,会首先调用这个函数

@Override

publicvoidonCreate(){

//TODOAuto-generatedmethodstub

super.onCreate();

System.out.println("ServiceonCreate");

}

@Override

publicintonStartCommand(Intentintent,intflags,intstartId){

//TODOAuto-generatedmethodstub

System.out.println("flags--->"+flags);

System.out.println("startId--->"+startId);

System.out.println("ServiceonStartCommand");

returnSTART_NOT_STICKY;

}

@Override

publicvoidonDestroy(){

//TODOAuto-generatedmethodstubo

System.out.println("ServiceonDestory");

super.onDestroy();

}

}

Service可以和Activity绑定,也可以和service绑定,service有两种绑定方法。这里拿Activityservice绑定为例。

1.第一种方法

1)activity

Intentintent=newIntent(MainActivity.this,BinderServiceTest.class);

bindService(intent,mConnection,Context.BIND_AUTO_CREATE);

//bindService最后一个参数为Context.BIND_AUTO_CREATE时会和service建立连接,会在连接建立之后调用onServiceConnected方法,bindService最后一个参数为Context.BIND_DEBUG_UNBIND时会断开和service的链接,会在断开连接后调用onServiceDisconnected()方法。

2)activity中实现个内部类,并override其方法

privateServiceConnectionmConnection=newServiceConnection(){

@Override

publicvoidonServiceConnected(ComponentNamename,IBinderservice){

//TODOAuto-generatedmethodstub

TestBinderBindService=(TestBinder)service;

Stringdata=BindService.getData();

Log.d("testBinder",data);

}

@Override

publicvoidonServiceDisconnected(ComponentNamename){

//TODOAuto-generatedmethodstub

Log.d("testBinder","Disconnected");

}

};

3)service

publicclassBinderServiceTestextendsService{

@Override

publicIBinderonBind(Intentarg0){

//TODOAuto-generatedmethodstub

IBinderbinder=newTestBinder();

returnbinder;

}

classTestBinderextendsBinder{

publicStringgetData(){

return"TestBinder";

}

}

}

2.第二种方法

1)activity

Intentintent=newIntent();

intent.setClass(TestActivity.this,FirstService.class);

startService(intent);

//startService(intent);方法时会调service中的onStartCommand()方法,当调stopService()方法时会调service中的onDestroy()方法。

2)service

publicclassFirstServiceextendsService{

@Override

publicIBinderonBind(Intentintent){

//TODOAuto-generatedmethodstub

System.out.println("ServiceonBind");

returnnull;

}

//当创建一个Servcie对象之后,会首先调用这个函数

@Override

publicvoidonCreate(){

//TODOAuto-generatedmethodstub

super.onCreate();

System.out.println("ServiceonCreate");

}

@Override

publicintonStartCommand(Intentintent,intflags,intstartId){

//TODOAuto-generatedmethodstub

System.out.println("flags--->"+flags);

System.out.println("startId--->"+startId);

System.out.println("ServiceonStartCommand");

returnSTART_NOT_STICKY;

}

@Override

publicvoidonDestroy(){

//TODOAuto-generatedmethodstubo

System.out.println("ServiceonDestory");

super.onDestroy();

}

}