访问本地服务里面的方法
原理图:
步骤:
调用服务里面的方法:
1 设计一个接口:IStudentQueryService Student queryStudent(int no);
2 在activity里面进行绑定操作:
bindService(intent,conn,flag);
3 写一个连接实现类:
private final class MyServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ibinder = (IStudnetQueryService) service;
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
conn = null;
ibinder = null;
}
}
4 在activity里面用IStudentQueryService 来接收onServiceConnected(ComponentName name, IBinder service)
返回过来的IBinder对象。
5 写一个StudentService extends Service ,重写onBind()
编译一个内部类StudentBinder extends Binder implements IStudnetQueryService
6 创建StudentBinder的对象,通过onBind()方法返回。
7 在activity通过onBind()返回的对象调用服务里面的方法。
代码如下:
1、main.xml
<service android:name="com.njupt.studentquery1.StudentService"/>