1.如果服務僅僅用于本地應用程序并且不必跨進程工作,則開發人員可以實現自己的Binder類來為客戶端提供訪問服務公共方法的方式,(注意:這僅僅當客戶端與服務位于同一個應用程序和進程時才有效,這也是最常見的情況,例如,音樂播放器需要綁定Activity到自己的服務來在后臺播放音樂)
2.實現步驟如下:
21.在服務這種,創建Binder類實例來完成下列操作之一;
211:包含客戶端能調用的公共方法
212:返回當前Service實例,其中包含客戶端能調用的 公共方法
213:返回服務管理的其他類實例,其中包含客戶端能調用的公共方法
22.從onBind()回調方法中返回Binder類實例
3.在客戶端,從onServiceConnected()回調方法接受Binder類實例,并且使用提供的方法調用綁定的服務
4:在Activity類中的代碼
package com.example.binderservice;
import com.example.binderservice.CurrentTimeService.LocalBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CurrentTimeActivity extends Activity {
CurrentTimeService cts;
boolean bound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onStart() {
super.onStart();
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(CurrentTimeActivity.this, CurrentTimeService.class);
bindService(intent, sc, BIND_AUTO_CREATE);//綁定服務
if (bound) {
Toast.makeText(CurrentTimeActivity.this, cts.getCurrentTime(),Toast.LENGTH_LONG).show();
}
}
});
}
protected void onStop() {
super.onStop();
bound=false;
unbindService(sc);
}
private ServiceConnection sc=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
bound=false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder binder=(LocalBinder)service;//獲得自定義的localBinder對象
cts= binder.geTimeService();//獲得CurrentTimeService對象
bound=true;
}
};
}
5.Service類中的代碼
package com.example.binderservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
public class CurrentTimeService extends Service{
private final IBinder binder = new LocalBinder();
public class LocalBinder extends Binder{
CurrentTimeService geTimeService(){
return CurrentTimeService.this;}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
public String getCurrentTime() {
Time time = new Time();
time.setToNow();
String currentTimeString=time.format("%Y-%m-%d %H:%M:%S");
return currentTimeString;
}
}
7.配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns: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>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".CurrentTimeService"></service>
</application>
</manifest>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。