基本步骤
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();
}