Android AIDL基本步骤

aidl 步骤
1 project- new aidl 文件,在src/main/aidl/底下
是一个接口文件
2 新建service 服务,新建 接口的庄对象,实现函数
private IBinder mIBinder = new IMyAidlInterface.Stub() {
@Override
public int getCount() throws RemoteException {
LogUtil. d ( TAG , "getCount:" + mCount );
return mCount ;
}
@Override
public void stopTimer() throws RemoteException {
LogUtil. d ( TAG , "stopTimer" );
if ( timer != null ) {
timer .cancel();
}
}
};
3 在服务onBind 方法中,将binder 返回
4 通过服务的bind 绑定
private IMyAidlInterface mAidl ;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
mAidl = IMyAidlInterface.Stub. asInterface (service);
}
public void onServiceDisconnected(ComponentName name) {
mAidl = null ;
}
};
Intent intent1 = new Intent(getApplicationContext(), AidlService. class );
bindService(intent1, mConnection , BIND_AUTO_CREATE );
绑定到服务,接收连接回调
连接上以后,转化拿到 aidl对象。断开时,置为空。


熟悉流程以后,主要是定义aidl 接口文件
然后做数据的交互

猜你喜欢

转载自blog.csdn.net/qq_42022061/article/details/80762492