AIDL记录
来源:互联网 发布:二叉树的递归遍历c语言 编辑:程序博客网 时间:2024/06/18 13:50
今天复习了下AIDL,做下整理。
概念:
AIDL (Android Interface Definition Language), Android接口定义语言,Android提供的IPC (Inter Process Communication,进程间通信)的一种独特实现。
注意点:
客户点写Intent时,格式最好是:(ComponentName(要找的Service的包名,要找到Service的全类名))
Intent intent=new Intent(); intent.setComponent(new ComponentName("com.hrx.kaka.aidltest","com.hrx.kaka.aidltest.MyService"));
因为在5.0之前可以使用Action找到跨进程的类,5.0之后不行(异常:java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=service })。
省略之前创建“anotherapp”项目的过程。。
省略要提供服务的“app”项目创建一个Service的过程。。。。
最终目的:使得anotherapp启动app的MyService,并进行通讯。
第一步:在“app”项目中创建一个aidl文件。
自动生成一个aidl文件,并写两个接口,一个用于接收数据,一个用于发送数据。
然后重构下项目
第二布:
在MyService里的onBind()方法中返回实现的Aidl方法。(具体啥方法可以在adil文件里定义)
第三布:
在”anotherapp”中绑定MyService。
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { Intent intent; private IAppServiceBinder binder =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(); intent.setComponent(new ComponentName("com.hrx.kaka.aidltest", "com.hrx.kaka.aidltest.MyService")); initView(); } private void initView() { findViewById(R.id.start_service).setOnClickListener(this); findViewById(R.id.stop_service).setOnClickListener(this); findViewById(R.id.bind_service).setOnClickListener(this); findViewById(R.id.unbind_service).setOnClickListener(this); findViewById(R.id.sync_service).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.start_service: startService(intent); break; case R.id.stop_service: stopService(intent); break; case R.id.bind_service: bindService(intent, this, Context.BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(this); break; case R.id.sync_service://传输数据到app if (binder!=null){ try { binder.setData("我传过来了"); } catch (RemoteException e) { e.printStackTrace(); } } break; } } //绑定的时候调用 @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("bind service"); binder=IAppServiceBinder.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { }}
第四步:
在要进行通讯的anotherapp项目也用同样的方法新建aidl,注意包名要与app项目的相同,然后直接把aidl文件复制进去。
第五步:
编写anotherapp调用aidl接口的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { Intent intent; private IAppServiceBinder binder =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(); intent.setComponent(new ComponentName("com.hrx.kaka.aidltest", "com.hrx.kaka.aidltest.MyService")); initView(); } private void initView() { findViewById(R.id.start_service).setOnClickListener(this); findViewById(R.id.stop_service).setOnClickListener(this); findViewById(R.id.bind_service).setOnClickListener(this); findViewById(R.id.unbind_service).setOnClickListener(this); findViewById(R.id.sync_service).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.start_service: startService(intent); break; case R.id.stop_service: stopService(intent); break; case R.id.bind_service: bindService(intent, this, Context.BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(this); break; case R.id.sync_service://传输数据到app if (binder!=null){ try { binder.getFromAnotherApp("我传过来了"); } catch (RemoteException e) { e.printStackTrace(); } try { System.out.println(binder.returnToFromAnotherApp()); } catch (RemoteException e) { e.printStackTrace(); } } break; } } //绑定的时候调用 @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("bind service"); binder=IAppServiceBinder.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { }}
仅仅只是对AIDL这块的使用做下记录。
刚刚看到网上有人讨论绑定Service解绑后不会停止的问题做下记录:
通过bindService启动的Service在unbindService后会停止。
通过startService启动再bindService进行绑定的Service不会停止,一定要调用stopService才会停止。
例子地址:http://download.csdn.net/detail/lang523493505/9577739
- AIDL记录
- AIDL使用方法记录
- android AIDL学习记录
- AIDL的学习记录
- Android AIDL进程通信 记录
- aidl通信的问题记录
- Android 进程间通信 AIDL学习记录
- 001安卓AIDL学习记录
- AIDL
- aidl
- AIDL
- AIDL
- AIDL
- AIDL
- AIDL
- aidl
- aidl
- AIDL
- 图解Linux命令之--more命令
- 手机设备端口9006刷回9008救砖步骤
- 一个字符串包含另一个字符串
- Contiki开发4:基于DEMO移植Contiki
- 二叉树的常见算法1
- AIDL记录
- openCV里的离散傅立叶变换
- 赋值到指定的内存地址
- 读书笔记-现代操作系统-4文件系统-4.3文件系统的实现
- 虚幻4:优化 CPU Profiling
- Cable master 小数二分
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- 一次关于Netty+Gson造成内存泄露的分析排查
- Web Services带来了什么