Android AIDL實(shí)現(xiàn)與服務(wù)相互調(diào)用方式
通過(guò)AIDL接口在進(jìn)程間傳遞數(shù)據(jù),記錄在開(kāi)發(fā)中遇到的一寫(xiě)問(wèn)題
AIDL支持?jǐn)?shù)據(jù)類(lèi)型如下:
1. Java 的原生類(lèi)型
2. String 和CharSequence
3. List 和 Map ,List和Map 對(duì)象的元素必須是AIDL支持的數(shù)據(jù)類(lèi)型; 以上三種類(lèi)型都不需要導(dǎo)入(import)
4. AIDL 自動(dòng)生成的接口 需要導(dǎo)入(import)
5. 實(shí)現(xiàn)android.os.Parcelable 接口的類(lèi). 需要導(dǎo)入(import)。
問(wèn)題1 在傳遞非基礎(chǔ)數(shù)據(jù)時(shí) 在參數(shù)前需加修飾符
void getDatas(in byte[] bs); void DataWhole(in PackageData data);}
這里重點(diǎn)是in、out、inout修飾符以及Parcelable的使用!常見(jiàn)的是in、Parcelable,少用的out、inout。
這幾種修飾符,可理解如下:
in:客戶(hù)端的參數(shù)輸入;
out:服務(wù)端的參數(shù)輸入;
inout:這個(gè)可以叫輸入輸出參數(shù),客戶(hù)端可輸入、服務(wù)端也可輸入??蛻?hù)端輸入了參數(shù)到服務(wù)端后,服務(wù)端也可對(duì)該參數(shù)進(jìn)行修改等,最后在客戶(hù)端上得到的是服務(wù)端輸出的參數(shù)。
問(wèn)題2 傳遞對(duì)象時(shí)的必要操作
1.必需實(shí)現(xiàn)Parcelable接口,內(nèi)部類(lèi)必需為靜態(tài)內(nèi)部類(lèi)
2.需在aidl目錄創(chuàng)建同類(lèi)名的AIDL文件,并聲明Parcelable,如圖
AIDL文件代碼就兩行
問(wèn)題3 參數(shù)大小的限制
如上在傳遞byte[] 長(zhǎng)度大于1024*1024時(shí)會(huì)拋出 TransactionTooLargeException 異常
問(wèn)題4 實(shí)現(xiàn)與服務(wù)之間互相調(diào)用
1.在綁定服務(wù)時(shí)會(huì)返回一個(gè)實(shí)現(xiàn)了AIDL的對(duì)象,這樣可以通過(guò)對(duì)象調(diào)用服務(wù)中對(duì)應(yīng)實(shí)現(xiàn),
2.可以在應(yīng)用層實(shí)現(xiàn)一個(gè)AIDL接口的對(duì)象,通過(guò)綁定服務(wù)返回的AIDL對(duì)象回傳給服務(wù),這樣可以在服務(wù)中主動(dòng)調(diào)用應(yīng)用層的方法實(shí)現(xiàn)數(shù)據(jù)回傳通知,
//接收回調(diào)INotification notification = new INotification.Stub() { @Override public void Datas(byte[] bs) throws RemoteException { Log.d(TAG, 'Datas: 收到數(shù)據(jù)=' + Arrays.toString(bs));//已測(cè)試 最大數(shù)據(jù)1024*1024 } }
//傳遞回調(diào)對(duì)象void setNotification(in INotification Notification);
@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { iAidlInterface = IAidlInterface.Stub.asInterface(service); try { iAidlInterface.setNotification(notification); } catch (RemoteException e) { e.printStackTrace(); }}
補(bǔ)充知識(shí):在Android系統(tǒng)中實(shí)現(xiàn)AIDL功能
之前實(shí)現(xiàn)AIDL的功能都是通過(guò)eclipse或者android studio工具實(shí)現(xiàn),最近由于項(xiàng)目需要,需要系統(tǒng)層提供接口給應(yīng)用層使用,所以想到使用AIDL。下面已一個(gè)非常簡(jiǎn)單的Demo來(lái)說(shuō)明在Android系統(tǒng)平臺(tái)生成AIDL的jar供應(yīng)用層使用。
一、AIDL的jar制作
首先新建一個(gè)android項(xiàng)目來(lái)用生產(chǎn)aidl的jar包,項(xiàng)目結(jié)構(gòu)如下:
gunder@gunder:/mnt/hgfs/ubuntuShare/aidl/SimpleJar$ tree.├── Android.mk└── src └── com └── china └── jar ├── IVoiceClientInterface.aidl └── VoiceManager.java
只有三個(gè)文件,首先看一下IVoiceClientInterface.aidl文件:
package com.china.jar; interface IVoiceClientInterface{ void face();}
里面只有一個(gè)簡(jiǎn)單的方法face。 IVoiceClientInterface.aidl主要是服務(wù)器端來(lái)實(shí)現(xiàn)的,而VoiceManager.java是供客戶(hù)端調(diào)用face方法使用的。VoiceManager.java具體實(shí)現(xiàn)如下:
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()){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_INIT_SERVICE:removeMessages(MSG_INIT_SERVICE);break; default:break;}}};}//獲取服務(wù)端注冊(cè)的NAME服務(wù)并跟服務(wù)端建立連接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;} //調(diào)用服務(wù)端的face方法,實(shí)現(xiàn)兩個(gè)不同app之間的進(jìn)程間通信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文件主要是用來(lái)將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 := simpleLOCAL_PACKAGE_NAME := SimpleServiceLOCAL_CERTIFICATE :=platforminclude $(BUILD_PACKAGE)
將該項(xiàng)目放置到android系統(tǒng)的packages/apps目錄單編就可以生產(chǎn)out/target/common/obj/JAVA_LIBRARIES/SimpleJar_intermediates/classes.jar,classes.jar就可以導(dǎo)入eclipse或者Android Studio中使用。
二、服務(wù)端實(shí)現(xiàn)AIDL中的接口demo目錄結(jié)構(gòu)如下:
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
主要實(shí)現(xiàn)只有5個(gè)文件:SimpleService.java、Logger.java、BootReceiverBroadcast.java、 Android.mk、 AndroidManifest.xml。SimpleService.java是實(shí)現(xiàn)AIDL的服務(wù),具體實(shí)現(xiàn)如下:
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;@Overridepublic IBinder onBind(Intent intent) {Logger.d();return mBinder;//跟客戶(hù)端綁定}@Overridepublic void onCreate() {super.onCreate();Logger.d();if (null == mBinder){initService();}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Logger.d();if (null == mBinder){initService();}return START_STICKY;}//實(shí)現(xiàn)AIDL的接口private class VoiceClientInterfaceImpl extends IVoiceClientInterface.Stub{@Overridepublic void face() throws RemoteException {Logger.d('face----excute!');//客戶(hù)端調(diào)用face方法時(shí)這里會(huì)執(zhí)行,會(huì)打印face----excute!}}//初始化服務(wù),主要是向系統(tǒng)注冊(cè)服務(wù)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的簡(jiǎn)單封裝,具體如下:
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是開(kāi)機(jī)完成的時(shí)候拉起 SimpleService服務(wù),具體實(shí)現(xiàn)如下:
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);//開(kāi)機(jī)啟動(dòng)會(huì)拉起服務(wù)SimpleService context.startService(service); }}
Android.mk具體實(shí)現(xiàn)如下:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src)LOCAL_PACKAGE_NAME := SimpleServiceLOCAL_CERTIFICATE :=platformLOCAL_PRIVILEGED_MODULE := falseLOCAL_DEX_PREOPT := falseLOCAL_STATIC_JAVA_LIBRARIES := simpleinclude $(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>
到這里服務(wù)端就實(shí)現(xiàn)完了。
三、客戶(hù)端實(shí)現(xiàn)AIDL的接口調(diào)用demo目錄結(jié)構(gòu)如下:
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個(gè)文件:Logger.java、 test.xml、TestVoice.java、Android.mk、AndroidManifest.xml,其中Logger.java跟服務(wù)端代碼一樣的。TestVoice.java的實(shí)現(xiàn)也很簡(jiǎn)單,在button調(diào)用face方法,具體實(shí)現(xiàn)如下:
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實(shí)現(xiàn)如下:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES := $(call all-java-files-under, src)LOCAL_STATIC_JAVA_LIBRARIES := simpleLOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := simple.jar #LOCAL_MODULE_TAGS :=optionalLOCAL_PACKAGE_NAME := Hello#LOCAL_CERTIFICATE :=platform#LOCAL_PRIVILEGED_MODULE := false#LOCAL_DEX_PREOPT := falseinclude $(BUILD_PACKAGE)
AndroidManifest.xml實(shí)現(xiàn)如下:
<?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>
到這里客戶(hù)端也實(shí)現(xiàn)了。將服務(wù)端跟客戶(hù)端的apk安裝到系統(tǒng)就可以測(cè)試了。
測(cè)試結(jié)果打印如下:
以上這篇Android AIDL實(shí)現(xiàn)與服務(wù)相互調(diào)用方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 如何在jsp界面中插入圖片6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. 詳解瀏覽器的緩存機(jī)制8. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. phpstudy apache開(kāi)啟ssi使用詳解
