【Service 1】Android Remote Service - AIDL
2015-12-04 11:21
615 查看
背景
Service作为Android的四大套件,其作用以及重要性都不必多说。在使用的过程中,UI模块(Activity)与Service的通讯是一个重要的话题。Activity和Service的关系可以分为两种:Activity与Service共享一个process,这种应该是最常见的情况。
Activity与Service不在同一个process中,如果它们在同一个app中但Service在manifest中注明了android:process=”:service_process_name”; 的话这个Service是会在另外一个process中启动的。如果它们不在同一个app中,那么自然就运行在不同的process。
本篇主要先分析第2中情况下的通讯,按照develop.android的文档说明,它们之间的通讯主要可以通过3种方式来实现:
1. AIDL 适用于需要跨进程而且需要支持多线程通讯,开销大
2. Binder 需要支持多线程通讯但是无需跨进程
3. Messenger 需要跨进程通讯但是无需考虑多线程
3种方式的实现复杂程度以及运行的开销都不一样,本着杀鸡不用牛刀的原则,应该按照自己app的需求来选择通讯方式。下面根据google的文档分析AIDL的使用以及需要注意的地方。
AIDL的线程模型:
对于同一个process中的调用,在同一个线程中执行。也就是说如果在UI线程中调用,则Service中的实现函数也会运行于UI线程。对于跨进程的调用,实现函数的执行不确定会在哪个线程执行,取决于系统的实现。
对于使用了”oneway”修饰的aidl接口,来自于其他进程调用的行为会被修改,变为直接返回。这个特性非常适合于remote callback。
AIDL的实现:
1,创建aidl文件。AIDL全称是Android Interface Definition Language,顾名思义这种语言是用于描述接口的。接口的实现者以及使用者都必须遵守这个“约定”,才能在彼此间正常通讯。AIDL描述的接口可以接受的数据类型包括java的基础类型,String, List, Map以及其他使用AIDL描述的接口(以参数形式传递remote callback给Service)。
可以使用in, out, inout修饰符定义参数的“方向”,从而优化性能。
示例代码如下,这个DEMO中我们定义了两个接口,IMyAidlInterface是Service负责实现的,用于描述Service可以提供的服务,通俗的说就是这个Service能干嘛。
// IMyAidlInterface.aidl package com.example.stone.listviewdemo.aidl; import com.example.stone.listviewdemo.aidl.IRemoteCallback; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); int getServicePid(in String prefix); void registerRemoteCallback(IRemoteCallback callback); void unregisterRemoteCallback(IRemoteCallback callback); }
IRemoteCallback是Client负责实现的,用于声明Client可以接收remote callback调用。
// IRemoteCallback.aidl package com.example.stone.listviewdemo.aidl; // Declare any non-default types here with import statements interface IRemoteCallback { oneway void onEvent(in String eventName); }
Service实现并暴露接口
实现IMyAidlInterface里面声明了的所有接口函数,打开IDE自动帮我们生成的IMyAidlInterface.java文件看看,其实Stub是android.os.Binder的子类。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 { Log.d(TAG, "anInt = " + anInt + "; aFloat = " + aFloat); } @Override public int getServicePid(String prefix) { int pid = 0; try { pid = Process.myPid(); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, e.getLocalizedMessage()); } Log.d(TAG, "prefix = " + prefix); return pid; } @Override public void registerRemoteCallback(IRemoteCallback callback) throws RemoteException { callbacks.add(callback); } @Override public void unregisterRemoteCallback(IRemoteCallback callback) throws RemoteException { Iterator<IRemoteCallback> iterator = callbacks.iterator(); while (iterator.hasNext()) { IRemoteCallback cb = iterator.next(); if (cb.equals(callback)) { iterator.remove(); } } } };
当Client使用bindService连接的时候,把这个实现返回给它
@Override public IBinder onBind(Intent intent) { Log.d(TAG, "somebody bind me with: " + intent.getAction()); return mBinder; }
如果你的Service会提供给其他app使用,那么在manifest.xml中要加入intent-filter声明
<service android:name=".services.HostService" android:enabled="true" android:process=":stone_host_service" android:exported="true" > <intent-filter> <action android:name="com.stonemobile.demo.bindService" /> </intent-filter> </service>
Client连接Service并使用接口
在ServiceConnection中获取返回的mBinderprivate IMyAidlInterface mAidlInterface; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.d(TAG, "Get iBinder done: " + iBinder); mAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); try { mAidlInterface.basicTypes(10, 123456789, true, 2.0f, 3.0f, "Hey man"); int pid = mAidlInterface.getServicePid("This is stone client"); // 注册回调 mAidlInterface.registerRemoteCallback(mCallback); Log.d(TAG, "Host service run with pid = " + pid); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { if (mAidlInterface != null) { try { mAidlInterface.unregisterRemoteCallback(mCallback); } catch (RemoteException e) { e.printStackTrace(); } } mAidlInterface = null; } };
所以当连接成功之后,我们就可以通过调用mAidlInterface的各个接口来使用Service所提供的服务了。
如果我们想使用Service的回调,则Client要实现remote callback接口:
private IRemoteCallback mCallback = new IRemoteCallback.Stub() { @Override public void onEvent(String eventName) throws RemoteException { Log.d(TAG, "onEvent: " + eventName); } };
记得我们的IRemoteCallback AIDL里面用了oneway描述onEvent()回调。这样的好处是Service调用onEvent后可以马上返回,就算Client在onEvent里面做了耗时的操作也不会阻塞Service,从而保证服务的质量。
连接相同app以及不同app中的Service的方法略有不同。如果要bind同一个app内的Service,设置intent的class即可:
Intent intent = new Intent(this, HostService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
要bind一个其他app内的Service:
private static final String HOST_ACTION_NAME = "com.stonemobile.demo.bindService"; Intent intent = new Intent(HOST_ACTION_NAME); bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
最后,一个简单的DEMO代码,包括两个项目。(请忽略里面的ListView内容)
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories