之前实现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配置文件如下:
到这里服务端就实现完了。
三、客户端实现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布局如下:
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实现如下:
到这里客户端也实现了。将服务端跟客户端的apk安装到系统就可以测试了。
测试结果打印如下: