1.如果服务仅仅用于本地应用程序并且不必跨进程工作,则开发人员可以实现自己的Binder类来为客户端提供访问服务公共方法的方式,(注意:这仅仅当客户端与服务位于同一个应用程序和进程时才有效,这也是最常见的情况,例如,音乐播放器需要绑定Activity到自己的服务来在后台播放音乐)

2.实现步骤如下:

21.在服务这种,创建Binder类实例来完成下列操作之一;

211:包含客户端能调用的公共方法

212:返回当前Service实例,其中包含客户端能调用的公共方法

213:返回服务管理的其他类实例,其中包含客户端能调用的公共方法

22.从onBind()回调方法中返回Binder类实例

3.在客户端,从onServiceConnected()回调方法接受Binder类实例,并且使用提供的方法调用绑定的服务

4:在Activity类中的代码

packagecom.example.binderservice;

importcom.example.binderservice.CurrentTimeService.LocalBinder;

importandroid.os.Bundle;
importandroid.os.IBinder;
importandroid.app.Activity;
importandroid.content.ComponentName;
importandroid.content.Intent;
importandroid.content.ServiceConnection;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.Toast;

publicclassCurrentTimeActivityextendsActivity{
CurrentTimeServicects;
booleanbound;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.activity_main,menu);
returntrue;
}
protectedvoidonStart(){
super.onStart();
Buttonbtn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(newOnClickListener(){

@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Intentintent=newIntent(CurrentTimeActivity.this,CurrentTimeService.class);
bindService(intent,sc,BIND_AUTO_CREATE);//绑定服务
if(bound){
Toast.makeText(CurrentTimeActivity.this,cts.getCurrentTime(),Toast.LENGTH_LONG).show();
}

}
});
}
protectedvoidonStop(){
super.onStop();
bound=false;
unbindService(sc);

}
privateServiceConnectionsc=newServiceConnection(){

@Override
publicvoidonServiceDisconnected(ComponentNamearg0){
//TODOAuto-generatedmethodstub
bound=false;
}

@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
//TODOAuto-generatedmethodstub
LocalBinderbinder=(LocalBinder)service;//获得自定义的localBinder对象
cts=binder.geTimeService();//获得CurrentTimeService对象
bound=true;

}
};
}

5.Service类中的代码

packagecom.example.binderservice;

importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.Binder;
importandroid.os.IBinder;
importandroid.text.format.Time;

publicclassCurrentTimeServiceextendsService{
privatefinalIBinderbinder=newLocalBinder();
publicclassLocalBinderextendsBinder{
CurrentTimeServicegeTimeService(){
returnCurrentTimeService.this;}
}
@Override
publicIBinderonBind(Intentintent){
//TODOAuto-generatedmethodstub
returnbinder;
}
publicStringgetCurrentTime(){
Timetime=newTime();
time.setToNow();
StringcurrentTimeString=time.format("%Y-%m-%d%H:%M:%S");
returncurrentTimeString;
}

}

7.配置文件

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.binderservice"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".CurrentTimeActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<serviceandroid:name=".CurrentTimeService"></service>
</application>

</manifest>


附件:http://down.51cto.com/data/2364291