Android进程通信:AIDL入门实例

AIDL即 Android Interface Definition Language。原因:On Android, one process cannot normally access thememory of another process.

也就是说AIDL用于android进程间通信,下面就记录一下第一个aidl的demo。

官方文档也给出了基本的使用方法,如下图:

技术分享


1. 在android项目的src相关包下创建一个aidl文件 IRemoteService.aidl:

package com.example.testndk;

interface IRemoteService {
    /** Request the process ID of this service, to do evil things with it. */
    int getPid();

    /** 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);
}
创建成功后会在gen目录自动生成一个同名的java文件,打开可以看到其实是一个接口

public interface IRemoteService extends android.os.IInterface

技术分享


2. 实现这个接口

package com.example.testndk;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
public class RemoteService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Return the interface
        return mBinder;
    }

    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getPid(){
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {
            // Does nothing
        }
    };
}

3. 把接口暴露给客户端,即调用处

package com.example.testndk;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 android.widget.TextView;

public class TestNdkActivity extends Activity {
    private static final String TAG = "TestNdkActivity";
    private TextView tv1 = null;

    // static {
    // System.loadLibrary("TestNdk");
    // }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        // String str = JniClient.AddStr("prefix", "suffix");
        //
        //
        // int iSum = JniClient.AddInt(5, 2);
        // String strSum = "5 + 2 = " + iSum;

        tv1 = new TextView(this);

        bindService(new Intent(RemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE);

        setContentView(tv1);
    }

    IRemoteService mIRemoteService = null;
    private ServiceConnection mConnection = new ServiceConnection() {
        // Called when the connection with the service is established
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Following the example above for an AIDL interface,
            // this gets an instance of the IRemoteInterface, which we can use
            // to call on the service
            mIRemoteService = IRemoteService.Stub.asInterface(service);
            try {
                tv1.setText("mIRemoteService=" + mIRemoteService.getPid());
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // Called when the connection with the service disconnects unexpectedly
        public void onServiceDisconnected(ComponentName className) {
            Log.e(TAG, "Service has unexpectedly disconnected");
            mIRemoteService = null;
        }
    };

    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }
}

用到了service,还需在AndroidManifest.xml配置一下,添加
        <service android:name="com.example.testndk.RemoteService">
            <intent-filter>
                <action android:name="com.example.testndk.RemoteService"/>
            </intent-filter>
        </service>

因为是通过action来查找service的,所以action标签不可少。

官方文档的例子稍微麻烦一点,不过也是比较清楚的。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。