Android中进程间通讯 AIDL
16lz
2021-01-23
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() { } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.e("MyService", "onCreate"); } @Override public void onDestroy() { Log.e("MyService", "onDestroy"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.e("MyService", "onUnbind"); return super.onUnbind(intent); } @Override public 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里代码:
@Override protected 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() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("MainActivity", service.toString()); mbinder = IMybinder.Stub.asInterface(service); } }; button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.example.ex0729_aidl_b");//通过隐式意图绑定Service bindService(intent , conn, BIND_AUTO_CREATE); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { unbindService(conn); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { mbinder.binderplay(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { mbinder.binderstop(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } @Override public 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; }
更多相关文章
- 通过Android的okhttp接口访问网络接口
- Android Studio 运行项目 报错误:You need to use a Theme.AppCom
- android实战项目二实现画板效果
- mtk android内置nginx服务器的方法
- ubuntu 编译 Android 出现的若干错误及解决方法
- 解决EventBus中接收方法中无法更新UI的问题
- Android保持屏幕常亮的三种方法
- Android .so abi兼容,通用armeabi-v7a和arm64-v8a架构的方法