android 实现aidl跨进程通信之一

来源:互联网 发布:分润系统源码 编辑:程序博客网 时间:2024/06/05 00:38

1,aidl通信实现步骤

本文基于android 6.0,是一个简单的例子,演示如何通过aidl进行跨进程通信。

目标:

1,创建2个进程,一个服务端,一个客户端。

2,客户端和服务端进行绑定,通过aidl跨进行调用以及回调。

1,创建2个工程

在packages/apps 路径下创建2个工程,

example:作为客户端  包名: com.android.example

exampleservice: 作为服务端  包名: com.android. exampleservice

2,aidl文件创建和编译

在2个工程的路径 src/com/android/aidl/ 下分别创建2个相同的aidl文件。

IObdData.aidl内容如下,

package com.android.aidl;import com.android.aidl.ICallback;interface IObdData {    String getData(String sr);    void sendMessage(String sr, ICallback callback);}

ICallback.aidl内容如下,

package com.android.aidl;interface ICallback {    void onGetResult(String info);}

aidl引用时必须添加路径。

 

将2个aidl文件加入2个工程的编译列表Android.mk中,

LOCAL_SRC_FILES += src/com/android/aidl/IObdData.aidl \                   src/com/android/aidl/ICallback.aidl

3,客户端

客户端的ExampleActivity.java文件内容如下,

package com.android.example;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import com.android.aidl.IObdData;import com.android.aidl.ICallback;public class ExampleActivity extends Activity {    private static String TAG = "Example ";        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                Intent intent = new Intent("com.android.exampleservice.aidl.ExampleService");        ComponentName component = new ComponentName("com.android.exampleservice",                "com.android.exampleservice.ExampleService");        intent.setComponent(component);        bindService(intent, conndata, BIND_AUTO_CREATE); // 绑定服务    }        private ServiceConnection conndata = new ServiceConnection() {                @Override        public void onServiceDisconnected(ComponentName name) {            Log.d(TAG, "onServiceDisconnected"); // 解绑        }        @Override        public void onServiceConnected(ComponentName name, IBinder service) { //已绑定            IObdData mObdData = IObdData.Stub.asInterface(service);            try {                String mobddata = mObdData.getData("mydata"); // 跨进程调用服务端的方法                Log.d(TAG,"mobddata " + mobddata);                                mObdData.sendMessage("sendMessage",mCallback); // 跨进程调用服务端的方法                            } catch (RemoteException e) {                e.printStackTrace();            }        }    };        private ICallback.Stub mCallback = new ICallback.Stub() {        @Override        public void onGetResult(String arg) throws RemoteException {// 跨进程回调            Log.d(TAG, "onGetResult = " + arg);        }    };        @Override    public void onStop() {        super.onStop();        unbindService(conndata);    }}

4,服务端

服务端的ExampleService.java文件如下,

package com.android.exampleservice;import android.app.Service;import android.os.Bundle;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.os.Handler;import android.os.Message;import android.os.RemoteException;import com.android.aidl.IObdData;import com.android.aidl.ICallback;public class ExampleService extends Service {    private ICallback mICallback; // 回调    private static String TAG = " exampleservice ";        @Override    public void onCreate() {        Log.d(TAG,"ExampleService: onCreate ");    }        private IObdData.Stub mDataBinder = new IObdData.Stub() {        @Override        public String getData(String sr){  // 实现getData方法            Log.d(TAG,"IObdData: getData " + sr);            return sr + "getData ";        }                @Override        public void sendMessage(String sr, ICallback mcallback){ // 实现sendMessage方法            Log.d(TAG,"IObdData: sendMessage " + sr);            mICallback = mcallback;                        Message msg = mHandler.obtainMessage();            msg.what = OUT_SEND;            mHandler.sendMessageDelayed(msg,5000);        }            };        public static final int OUT_SEND = 10;    public final Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {            case OUT_SEND:                try {                    mICallback.onGetResult("sendMessage success");// 回调                } catch (RemoteException e) {                    e.printStackTrace();                }                break;            }        }    };        @Override    public IBinder onBind(Intent intent) {        return mDataBinder;    }}

这里延时5s是模仿耗时操作。

工程的其他内容就不多说了,在系统源码环境下编译,然后安装到机器上就可以运行。

0 0