日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中进程间通讯 AIDL

發布時間:2025/5/22 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中进程间通讯 AIDL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android中進程間通訊 AIDL
IDL Interface Description Language ?接口描述語言
AIDL Android IDL

適用場景:
? ?client進程必須是Activity,服務端進程必須是Service


aidl解決兩個項目間通訊(IPC進程間通訊)一個項目里必須有service。另一個項目的activity調用service里的方法需要通過一個共有的接口

步驟:
1.在_b項目中創建一個接口,里面的方法是_b中service里面的內部類Mybinder的binderplay()方法
2.到文件夾的目錄里把創建的這個接口改后綴名為.aidl
3.去掉接口的public修飾
4.會在_b的gen目錄下生成一個.java的接口文件,把這個拷貝到_a的項目里,兩個必須是相同的包名。
5.在_b的內部類Mybinder中繼承這個接口.stub
6.在_a的activity的onServiceConnected()方法里,把傳來的service強制轉換成接口類型
? ?例:mbinder = IMybinder.Stub.asInterface(service);
7.就可以用生成mbinder對象來調用service中的方法


代碼如下:

_b項目中創建接口:

package com.example.ex0729_aidl_b; interface IMybinder {void binderplay();void binderstop(); }

_b項目中的sevice代碼:

public class MyService extends Service {class Mybinder extends IMybinder.Stub{public void binderplay(){MyService.this.play();}public void binderstop(){MyService.this.stop();}}public MyService(){}@Overridepublic void onCreate(){// TODO Auto-generated method stubsuper.onCreate();Log.e("MyService", "onCreate");}@Overridepublic void onDestroy(){Log.e("MyService", "onDestroy");super.onDestroy();}@Overridepublic boolean onUnbind(Intent intent){// TODO Auto-generated method stubLog.e("MyService", "onUnbind");return super.onUnbind(intent);}@Overridepublic IBinder onBind(Intent intent){Log.e("MyService", "onBind");return new Mybinder();}private void play(){Log.e("MyService", "play()");}public void stop(){Log.e("MyService", "stop()");} }


_a項目中的Activity里代碼:

@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button1 = (Button) findViewById(R.id.button1);Button button2 = (Button) findViewById(R.id.button2);Button button3 = (Button) findViewById(R.id.button3);Button button4 = (Button) findViewById(R.id.button4);conn = new ServiceConnection(){@Overridepublic void onServiceDisconnected(ComponentName name){// TODO Auto-generated method stub}@Overridepublic void onServiceConnected(ComponentName name, IBinder service){Log.e("MainActivity", service.toString());mbinder = IMybinder.Stub.asInterface(service);}};button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){Intent intent = new Intent();intent.setAction("com.example.ex0729_aidl_b");//通過隱式意圖綁定ServicebindService(intent , conn, BIND_AUTO_CREATE);}});button2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){unbindService(conn);}});button3.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){try{mbinder.binderplay();}catch (RemoteException e){// TODO Auto-generated catch blocke.printStackTrace();}}});button4.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){try{mbinder.binderstop();}catch (RemoteException e){// TODO Auto-generated catch blocke.printStackTrace();}}});}@Overridepublic 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;}


轉載于:https://blog.51cto.com/wangcuijing/1274666

總結

以上是生活随笔為你收集整理的Android中进程间通讯 AIDL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。