先分析本地同进程的服务访问
通过输入no查询名字
布局
新建3个类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etno = (EditText) findViewById(R.id.editText1);
etname = (EditText) findViewById(R.id.editText2);
Intent intent = new Intent(this, StudentsService.class);
bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
private EditText etno;
private EditText etname;
private IStudent istudent = null;
private Myconnection connection = new Myconnection();
public class Myconnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder b) {
istudent = (IStudent) b;
}
@Override
public void onServiceDisconnected(ComponentName name) {
istudent = null;
}
}
public void searCh(View v) {
String no = etno.getText().toString();
String name = istudent.queryStudent(Integer.parseInt(no));
etname.setText(name);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unbindService(connection);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class StudentsService extends Service {
private String[] name = { "kity", "mary" };
private String query(int no) {
if (no <= 2) {
return name[no - 1];
}
return "查无此人";
}
IBinder binder = new MyIBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class MyIBinder extends Binder implements IStudent {
@Override
public String queryStudent(int no) {
return query(no);
}
}
}
服务要在清单配置文件中注册
接口回调实现服务器访问
public interface IStudent {
public String queryStudent(int no);
}
然后开始跨进程访问
先建一个服务端
aidl文件会自动在gen生成java,注意aidl中不可以有访问访问修饰符;
package com.example.aidl;
interface IStudent {
String queryStudent(int i);
}
public class StudentService extends Service {
private String[] names = { "aaa", "bbb", "ccc" };
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return ibinder;
}
private String query(int no) {
if (no >= 1 && no <= 5) {
return names[no - 1];
}
return null;
}
MyIBinder ibinder = new MyIBinder();
public class MyIBinder extends IStudent.Stub {//注意stub,同进程中是IStudent
@Override
public String queryStudent(int i) throws RemoteException {
return query(i);
}
}
}
注册
<service android:name="com.example.aidl_service.StudentService">
<intent-filter >
<action android:name="com.remote.QUERY_STUDENT"/>
</intent-filter>
</service>
编写客户端
注意aidl包是从服务端连包带aidl文件一起拷贝过来的,同样会在根目录自动生成java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etno = (EditText) findViewById(R.id.eton_client);
etname = (EditText) findViewById(R.id.etcontent_client);
Intent intent = new Intent();
intent.setAction("com.remote.QUERY_STUDENT");
bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
private EditText etno;
private EditText etname;
private IStudent istudent = null;
private Myconnection connection = new Myconnection();
public class Myconnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder b) {
//istudent = (IStudent) b;
//此处要特别注意,上面是同进程中访问服务时候获取传过来的IBinder实例
istudent = IStudent.Stub.asInterface(b);
}
@Override
public void onServiceDisconnected(ComponentName name) {
istudent = null;
}
}
public void searCh(View v) throws NumberFormatException, RemoteException {
String no = etno.getText().toString();
String name = istudent.queryStudent(Integer.parseInt(no));
etname.setText(name);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unbindService(connection);
}
}
运行的时候先开启服务端,在开启客户端;