Android AIDL实现与服务相互调用方式
作者:bug毛毛虫
通过AIDL接口在进程间传递数据,记录在开发中遇到的一写问题
AIDL支持数据类型如下:
1. Java 的原生类型
2. String 和CharSequence
3. List 和 Map ,List和Map 对象的元素必须是AIDL支持的数据类型; 以上三种类型都不需要导入(import)
4. AIDL 自动生成的接口 需要导入(import)
5. 实现android.os.Parcelable 接口的类. 需要导入(import)。
问题1 在传递非基础数据时 在参数前需加修饰符
void getDatas(in byte[] bs); void DataWhole(in PackageData data); }
这里重点是in、out、inout修饰符以及Parcelable的使用!常见的是in、Parcelable,少用的out、inout。
这几种修饰符,可理解如下:
in:客户端的参数输入;
out:服务端的参数输入;
inout:这个可以叫输入输出参数,客户端可输入、服务端也可输入。客户端输入了参数到服务端后,服务端也可对该参数进行修改等,最后在客户端上得到的是服务端输出的参数。
问题2 传递对象时的必要操作
1.必需实现Parcelable接口,内部类必需为静态内部类
2.需在aidl目录创建同类名的AIDL文件,并声明Parcelable,如图
AIDL文件代码就两行
问题3 参数大小的限制
如上在传递byte[] 长度大于1024*1024时会抛出 TransactionTooLargeException 异常
问题4 实现与服务之间互相调用
1.在绑定服务时会返回一个实现了AIDL的对象,这样可以通过对象调用服务中对应实现,
2.可以在应用层实现一个AIDL接口的对象,通过绑定服务返回的AIDL对象回传给服务,这样可以在服务中主动调用应用层的方法实现数据回传通知,
//接收回调 INotification notification = new INotification.Stub() { @Override public void Datas(byte[] bs) throws RemoteException { Log.d(TAG, "Datas: 收到数据=" + Arrays.toString(bs));//已测试 最大数据1024*1024 } }
//传递回调对象 void setNotification(in INotification Notification);
@Override public void onServiceConnected(ComponentName name, IBinder service) { iAidlInterface = IAidlInterface.Stub.asInterface(service); try { iAidlInterface.setNotification(notification); } catch (RemoteException e) { e.printStackTrace(); } }
补充知识:在Android系统中实现AIDL功能
之前实现AIDL的功能都是通过eclipse或者android studio工具实现,最近由于项目需要,需要系统层提供接口给应用层使用,所以想到使用AIDL。下面已一个非常简单的Demo来说明在Android系统平台生成AIDL的jar供应用层使用。
一、AIDL的jar制作
首先新建一个android项目来用生产aidl的jar包,项目结构如下:
gunder@gunder:/mnt/hgfs/ubuntuShare/aidl/SimpleJar$ tree . ├── Android.mk └── src └── com └── china └── jar ├── IVoiceClientInterface.aidl └── VoiceManager.java
只有三个文件,首先看一下IVoiceClientInterface.aidl文件:
package com.china.jar; interface IVoiceClientInterface{ void face(); }
里面只有一个简单的方法face。 IVoiceClientInterface.aidl主要是服务器端来实现的,而VoiceManager.java是供客户端调用face方法使用的。VoiceManager.java具体实现如下:
package com.china.jar; import com.china.jar.IVoiceClientInterface; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.os.RemoteException; import android.util.Log; import android.os.ServiceManager; public class VoiceManager { private static final String TAG = "VoiceManager"; private static VoiceManager mVoiceManager; private static IVoiceClientInterface mService = null; public static final String NAME = "simple_jar"; public static final boolean DEBUG_DATA = true; private final HandlerThread mWorkThread; private final Handler mWorkHander; private static final int MSG_INIT_SERVICE = 0x01; //单例模式 public static synchronized VoiceManager getInstance(){ if (null == mVoiceManager){ synchronized (VoiceManager.class) { if (null == mVoiceManager){ mVoiceManager = new VoiceManager(); } } } return mVoiceManager; } private VoiceManager(){ mWorkThread = new HandlerThread("simple_manager"); mWorkThread.start(); mWorkHander = new Handler(mWorkThread.getLooper()){ @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_INIT_SERVICE: removeMessages(MSG_INIT_SERVICE); break; default: break; } } }; } //获取服务端注册的NAME服务并跟服务端建立连接 private synchronized IVoiceClientInterface getService(){ if (null == mService){ Log.e(TAG, "IVocieService init"); mService = IVoiceClientInterface.Stub.asInterface(ServiceManager .getService(NAME)); } if (null == mService){ Log.e(TAG, "jar service is null"); mWorkHander.removeMessages(MSG_INIT_SERVICE); mWorkHander.sendEmptyMessageDelayed(MSG_INIT_SERVICE, 100); } return mService; } //调用服务端的face方法,实现两个不同app之间的进程间通信 public void face(){ Log.d(TAG, "face"); mService = getService(); if (null == mService){ Log.e(TAG, "face mService is null!"); return ; } try{ mService.face(); }catch(RemoteException e){ e.printStackTrace(); } } }
Android.mk文件主要是用来将IVoiceClientInterface.aidl和VoiceManager.java编译成jar包,以方便在eclipse或者Android Studio中使用。
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_JAVA_LIBRARIES := simple LOCAL_PACKAGE_NAME := SimpleService LOCAL_CERTIFICATE :=platform include $(BUILD_PACKAGE)
将该项目放置到android系统的packages/apps目录单编就可以生产out/target/common/obj/JAVA_LIBRARIES/SimpleJar_intermediates/classes.jar,classes.jar就可以导入eclipse或者Android Studio中使用。
二、服务端实现AIDL中的接口demo目录结构如下:
gunder@gunder:/mnt/hgfs/ubuntuShare/aidl/SimpleJarService$ tree
.
├── AndroidManifest.xml
├── Android.mk
├── libs
│ └── simple.jar
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── layout
│ ├── values
│ │ ├── strings.xml
│ │ └── styles.xml
│ ├── values-v11
│ │ └── styles.xml
│ └── values-v14
│ └── styles.xml
└── src
└── com
└── china
└── service
├── BootReceiverBroadcast.java
├── Logger.java
└── SimpleService.java
主要实现只有5个文件:SimpleService.java、Logger.java、BootReceiverBroadcast.java、 Android.mk、 AndroidManifest.xml。SimpleService.java是实现AIDL的服务,具体实现如下:
package com.china.service; import com.china.jar.IVoiceClientInterface; import com.china.jar.VoiceManager; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; public class SimpleService extends Service{ private static VoiceClientInterfaceImpl mBinder; @Override public IBinder onBind(Intent intent) { Logger.d(); return mBinder;//跟客户端绑定 } @Override public void onCreate() { super.onCreate(); Logger.d(); if (null == mBinder){ initService(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Logger.d(); if (null == mBinder){ initService(); } return START_STICKY; } //实现AIDL的接口 private class VoiceClientInterfaceImpl extends IVoiceClientInterface.Stub{ @Override public void face() throws RemoteException { Logger.d("face----excute!");//客户端调用face方法时这里会执行,会打印face----excute! } } //初始化服务,主要是向系统注册服务 private void initService(){ Logger.d(); if (null == mBinder){ synchronized (SimpleService.class) { if (null == mBinder){ try { mBinder = new VoiceClientInterfaceImpl(); ServiceManager.addService(VoiceManager.NAME, mBinder); } catch (Exception e) { e.printStackTrace(); } } } } } }
Logger.java是打印Log的简单封装,具体如下:
package com.china.service; import android.util.Log; import java.util.Locale; public class Logger { public static final boolean DEBUG = true; public static final String DEFAULT_TAG = "AIOS_"; public Logger(){} public static void d(){ if (DEBUG){ Log.d(DEFAULT_TAG,getPrefix()); } } public static void d(String msg){ if (DEBUG){ Log.d(DEFAULT_TAG, getPrefix() + msg); } } public static void d(String msg, Throwable tr){ if (DEBUG){ Log.d(DEFAULT_TAG, getPrefix() + msg, tr); } } private static String getPrefix(){ StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4]; String className = stackTraceElement.getClassName(); int classNameStartIndex = className.lastIndexOf(".") + 1; className = className.substring(classNameStartIndex); String methodName = stackTraceElement.getMethodName(); int methodLine = stackTraceElement.getLineNumber(); String format = "%s_%s(L:%d)"; return String.format(Locale.CANADA, format, className, methodName, methodLine); } }
BootReceiverBroadcast.java是开机完成的时候拉起 SimpleService服务,具体实现如下:
package com.china.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootReceiverBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Logger.d(); Intent service = new Intent(context, SimpleService.class);//开机启动会拉起服务SimpleService context.startService(service); } }
Android.mk具体实现如下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_PACKAGE_NAME := SimpleService LOCAL_CERTIFICATE :=platform LOCAL_PRIVILEGED_MODULE := false LOCAL_DEX_PREOPT := false LOCAL_STATIC_JAVA_LIBRARIES := simple include $(BUILD_PACKAGE) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=simple:libs/simple.jar include $(BUILD_MULTI_PREBUILT) include $(call all-makefiles-under,$(LOCAL_PATH))
这里的simple.jar是第一步中制作的classes.jar。 AndroidManifest.xml配置文件如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chinatsp.service" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name="com.china.service.SimpleService"></service> <receiver android:name="com.china.service.BootReceiverBroadcast"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- <category android:name="android.intent.category.LAUNCHER"/> --> </intent-filter> </receiver> </application> </manifest>
到这里服务端就实现完了。
三、客户端实现AIDL的接口调用demo目录结构如下:
gunder@gunder:/mnt/hgfs/ubuntuShare/aidl/SimpleJarClient$ tree
.
├── AndroidManifest.xml
├── Android.mk
├── libs
│ └── simple.jar
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── layout
│ │ ├── activity_main.xml
│ │ ├── activity_tss.xml
│ │ └── test.xml
│ ├── menu
│ ├── values
│ │ ├── dimens.xml
│ │ └── strings.xml
│ ├── values-v11
│ ├── values-v14
│ └── values-w820dp
│ └── dimens.xml
└── src
└── com
└── example
└── helloworld
├── TestVoice.java
└── util
└── Logger.java
这里主要看5个文件:Logger.java、 test.xml、TestVoice.java、Android.mk、AndroidManifest.xml,其中Logger.java跟服务端代码一样的。TestVoice.java的实现也很简单,在button调用face方法,具体实现如下:
package com.example.helloworld; import android.app.Activity; import android.os.Bundle; import android.view.View; import com.example.helloworld.util.Logger; public class TestVoice extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); } public void startVoice(View view){ Logger.d(); } public void stopVoice(View view){ Logger.d(); com.china.jar.VoiceManager.getInstance().face(); } public void finishVoice(View view){ Logger.d(); finish(); } }
test.xml布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startVoice" android:text="@string/tts_start"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stopVoice" android:text="@string/tts_stop"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="finishVoice" android:text="@string/tts_finish"/> </LinearLayout>
Android.mk实现如下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_STATIC_JAVA_LIBRARIES := simple LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := simple.jar #LOCAL_MODULE_TAGS :=optional LOCAL_PACKAGE_NAME := Hello #LOCAL_CERTIFICATE :=platform #LOCAL_PRIVILEGED_MODULE := false #LOCAL_DEX_PREOPT := false include $(BUILD_PACKAGE)
AndroidManifest.xml实现如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="com.example.helloworld.TestVoice" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
到这里客户端也实现了。将服务端跟客户端的apk安装到系统就可以测试了。
测试结果打印如下:
以上这篇Android AIDL实现与服务相互调用方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。