服务端:
- 建立一个AIDL文件 会自动生成一个相关的java文件
AIDL里的接口名要和AIDL的文件名一致 - 建立Myservice类继承Service
建立一个MyBinder类继承Stub,这个Stub(存根)是上一步自动生成的java文件里的一个内部类,Stub这个内部类里包含了AIDL里定义的接口
MyBinder类继承了这个Stub之后要实现里面的方法
3.在Myservice的onBinder()方法里返回一个MyBinder对象。客户端会通过这个MyBinder对象的代理来和服务端通信。
客户端:
- 建立一个和服务端一模一样的AIDL文件,包括包名也要一致
创建一个service对象,比如是private IAdditionService Addservice;
创建一个ServiceConnection对象,并重写里面的两个方法:
@Override
public void onServiceDisconnected(ComponentName arg0) {
// 这个方法会在断开绑定的时候调用
Addservice = null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
//这个方法会在绑定成功后调用
Addservice = IAdditionService.Stub.asInterface(service);
//上面这行函数非常重要,它就是获得了服务端IBinder对象的一个代理对象。
}
//重要:
Addservice = IAdditionService.Stub.asInterface(service); 这行代码就会获得服务器端onBind()方法返回的binder对象的 代理!代理!代理哦!
在oncreate()方法里绑定服务端的service
Intent i = new Intent();
i.setComponent(new ComponentName(
“com.example.hellosumaidl”, //指定 包名
“com.example.hellosumaidl.AdditionService”));//指定 包名.类名
bindService(i, conn, Service.BIND_AUTO_CREATE);//这一步里就会获得服务端的binder代理
通过得到的代理对象Addservice .fun()就可以使用服务端的方法了;
全代码:
服务端:
aidl文件:
package com.example.aidl;
interface IAdditionService{
int add(in int num1,in int num2);
}
package com.example.hellosumaidl;
自定义service类:
public class AdditionService extends Service {
private MyBinder binder = null;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
binder = new MyBinder();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
binder = null;
}
public class MyBinder extends Stub
{
@Override
public int add(int num1, int num2) throws RemoteException {
// TODO Auto-generated method stub
return num1+num2;
}
}
}
客户端:
public class MainActivity extends Activity {
private Button button;
private EditText num1, num2;
private TextView result;
private IAdditionService Addservice;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
Addservice = null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
Addservice = IAdditionService.Stub.asInterface(service);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCalc);
num1 = (EditText) findViewById(R.id.value1);
num2 = (EditText) findViewById(R.id.value2);
result = (TextView) findViewById(R.id.result);
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.hellosumaidl", "com.example.hellosumaidl.AdditionService"));
bindService(i, conn, Service.BIND_AUTO_CREATE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int v1, v2, res = -1;
v1 = Integer.parseInt(MainActivity.this.num1.getText().toString());
v2 = Integer.parseInt(MainActivity.this.num2.getText().toString());
try {
res = Addservice.add(v1, v2);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.setText(res + "");
Log.i("AIDLLLL", v1 +" + "+v2+" = "+res+"");
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
//这个实例是客户端通过调用服务端的方法来计算两个数的和
关键步骤:
服务端: 建立自己的binder类并继承Stub,然后实现里面的方法,在onBinder()返回自定义的binder对象。
客户端: 建立和服务端一模一样的aidl文件(包括包名)
显示intent调用服务端service (要指定 包名,包名.类名)
建立ServiceConnection对象,复写里面的两个方法,在onServiceConnected()方法里
调用 IAdditionService.Stub.asInterface(service);获得binder代理。
之后就可以使用这个binder代理啦
另:
一个APP 启动停止另一个APP的服务
直接使用startservice stopservice就可以
5.0之前可以使用显式和隐式Intent的方式调用
但是5.0之后只能通过显式的Intent调用另一个app的activity或者service