基本步驟
1、構造查詢界面:一個輸入框 + 一個按鈕 + 一個顯示框
2、在Activity的onCreate()方法:添加按鈕點擊事件
3、定義一個數據查詢接口IStudent的一個方法 queryStudent()
4、新建Service子類StudentService,在其內部定義一個內部類StundentBinder,
StundentBinder實現IStudent並繼承Binder。 >>>>此步是重中之重,非常關鍵。
5、實現StudentService的onBind(),和具體的查詢方法
6、在Activity中新建一個內部類,實現ServiceConnection接口,
添加未實現的方法onServiceConnected和onServiceDisconnected(暫不添加處理邏輯)
7、在Activity中添加IStundent的屬性iStundent,在方法onServiceConnected和onServiceDisconnected進行賦值和銷燬
8、在Activity中添加ServiceConnection的屬性conn,並在onCreate()中實現bindService(),
conn是bindService()所必須的參數。
9、在點擊事件處理邏輯中,調用iStundent的查詢方法,從服務中獲得數據。
10、在Activity中覆寫onDestroy,調用unbindService()
===================================================================
下面我們附加上代碼:
1、構造查詢界面:一個輸入框 + 一個按鈕 + 一個顯示框
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/studentno" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/resultView" />
2、在Activity的onCreate()方法:添加按鈕點擊事件
resultView = (TextView) this.findViewById(R.id.resultView);
studentno = (EditText) this.findViewById(R.id.studentno);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
3、定義一個數據查詢接口IStudent的一個方法 queryStudent()
public interface IStundent {
public String queryStudent(int no);
}
4、新建Service子類StudentService,在其內部定義一個內部類StundentBinder,
StundentBinder實現IStudent並繼承Binder。 >>>>此步是重中之重,非常關鍵。
public class StudentService extends Service{
private class StundentBinder extends Binder implements IStundent{
public String queryStudent(int no) {
return query(no);
}
}
}
5、實現StudentService的onBind(),和具體的查詢方法
public String query(int no){
String[] names = {"張飛","李小龍","趙薇"};
if(no>0 && no<4){
return names[no - 1];
}
return null;
}
@Override
public IBinder onBind(Intent intent) {
return new StundentBinder();
}
6、在Activity中新建一個內部類,實現ServiceConnection接口,
添加未實現的方法onServiceConnected和onServiceDisconnected(暫不添加處理邏輯)
private class StudentServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
}
public void onServiceDisconnected(ComponentName name) {
}
}
7、在Activity中添加IStundent的屬性iStundent,在方法onServiceConnected和onServiceDisconnected進行賦值和銷燬
private IStundent iStundent;
public void onServiceConnected(ComponentName name, IBinder service) {
iStundent = (IStundent)service;
}
public void onServiceDisconnected(ComponentName name) {
iStundent = null;
}
8、在Activity中添加ServiceConnection的屬性conn,並在onCreate()中實現bindService(),
conn是bindService()所必須的參數。
private ServiceConnection conn = new StudentServiceConnection();
Intent intent = new Intent(this, StudentService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
9、在點擊事件處理邏輯中,調用iStundent的查詢方法,從服務中獲得數據。
public void onClick(View v) {
String no = studentno.getText().toString();
String name = iStundent.queryStudent(Integer.valueOf(no));
resultView.setText(name);
}
10、在Activity中覆寫onDestroy,調用unbindService()
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}