- 2.在aApp中创建AIDL接口写法跟java代码类似,但是这里有一点值得注意的就是它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口
- 定义接口
-
interface My_Service_Int {
String str(String str);
}
- 3.在Service 中重写onBind()方法 返回IBinder,我们需要自定义类 继承我们的AIDL接口.Stub
-
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("My_Serve", "绑定_service_Bind");
return new MyService();
}
- 4.重写接口中的方法
-
public class MyService extends My_Service_Int.Stub {
@Override
public String str(String str) throws RemoteException {
raidBooke(str);
return null;
}
}
- 5.在bApp中 创建AIDL接口 在个接口中的包名文件名内容必须一致
- 绑定隐士启动Service
-
intents = new Intent("my_menu_seaver");
intents.setPackage("k.com.my_android_serve_one");
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("MainActivity", "bind_关联");
//AIDL接口调用asInterface(Binder) 获取到接口对象
my_service_int = My_Service_Int.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("MainActivity", "取消关联");
}
};
-
bindService(intents, connection, BIND_AUTO_CREATE);
-
try { //调用接口中的方法,为其赋值
my_service_int.str("新闻联播");
} catch (RemoteException e) {
e.printStackTrace();
}
- 与接口回掉很相似