Aidl開發流程(android studio)

1,server端:新建aidl文件IMyInterface,在aidl文件中聲明功能方法。
2,執行 build ->make project
3,新建ServerService繼承Service,在onBind方法中實現 IMyInterface.stub並返回:
public IBinder onBind(Intent intent) {

return new IMyAidlInterface.Stub() {
@Override
public void showHello() throws RemoteException {
System.out.println("------Hello World");
}
};
}

4,在activity中通過intent啓動service,在清單文件中配置service
            android:name=".BinderServer">
           
               
           
       
5,運行到手機上

客戶端:
1,新建項目,將上文 的aidl文件複製到build\generated\source\aidl\debug\目 錄下,包名保持和服務端一致
2,activity中,通過服務端配置的action隱式啓動service
  Intent intent = new Intent();
intent.setAction("hello.world.service");
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

myBinder = IMyAidlInterface.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
myBinder = null;
}
}, BIND_AUTO_CREATE);

3,通過myBinder調用服務端service中實現的方法。


發佈了32 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章