Android:AS环境下用AIDL进行IPC
2015-07-21 20:55
507 查看
本文主要讲的是如何在AS环境下通过AIDL进行IPC,而不是主要讲AIDL和IPC,这部分内容可以参考其他文章,或者官方文档http://developer.android.com/guide/components/aidl.html。因为自己在网上看到的实践都是Eclipse环境下的,所以试了下AS,分享给大家。
简单地说AIDL是Android实现IPC的一种方式。
Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
只有当你需要不同应用访问你的服务并且需要处理多线程问题时,才使用AIDL,否则应该直接继承Binder或者使用Messenger。
Step1:在server端新建一个aidl文件
这时AS自动生成一个包和aidl文件
声明这个aidl文件的接口
Step2:make project
Step3:写一个Service类
Step4:在Service节点下添加一个action节点
step5:复制该目录到Client Module/src/main
step6:Client Module进行make project
step7:bindService for IPC
End
什么是AIDL
AIDL allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).简单地说AIDL是Android实现IPC的一种方式。
Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
只有当你需要不同应用访问你的服务并且需要处理多线程问题时,才使用AIDL,否则应该直接继承Binder或者使用Messenger。
AS环境下使用AIDL
AS version 1.2.2Step1:在server端新建一个aidl文件
这时AS自动生成一个包和aidl文件
声明这个aidl文件的接口
Step2:make project
Step3:写一个Service类
[code]package com.example.kenjxli.aidlserver; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyService extends Service { private IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public int add(int a, int b) throws RemoteException { return a + b; } }; public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. Log.e("TAG","onBind"); return mBinder; } @Override public void onDestroy() { super.onDestroy(); Log.e("TAG", "onDestory"); } }
Step4:在Service节点下添加一个action节点
[code] <service android:name=".MyService"> <intent-filter> <action android:name="com.example.kenjxli.aidlserver"></action> </intent-filter> </service>
step5:复制该目录到Client Module/src/main
这是关键一步,直接复制Server Module在Step2中生成的aidl包到,Client Module/src/main即可。
step6:Client Module进行make project
step7:bindService for IPC
[code]public class MainActivity extends ActionBarActivity { private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { IMyAidlInterface aidl = IMyAidlInterface.Stub.asInterface(service); try { int result = aidl.add(1, 2); Log.e("TAG", result + ""); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent("com.example.kenjxli.aidlserver"); bindService(intent, connection, BIND_AUTO_CREATE); } }
End
相关文章推荐
- Android 4.0屏蔽式多点触摸
- Android 关于定位
- android 之 Intent 传递数据大小有限的问题
- Android 异步加载
- [Android]自定义ListView:上拉加载更多
- Android ExpandMenu 多级菜单
- AndroidAnnotations库的使用
- Android+OpenCV上手程序
- android studio乱码解决方案
- android开发-简单基础学习
- android遮挡系统拨号界面 类似360来电显示的那个绿色 页面
- Android Toast几种用法
- Android浮动小球与开机自启动
- Android 安卓点击运行后 出错
- Exploring the world of Android :: Part 2
- Android通知的基本用法
- Exploring the world of Android :: Part 1
- android studio gradle download 无反应
- Android设计模式之美---Factory Method
- Android Wear(手表)开发 - 学习指南