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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 借助Ant工具,实现快速开发
- 下一篇: 左边腰疼是什么原因引起的?