1 AIDL 简介

        AIDL(Android Interface Definition Language)是一种接口定义语言,用于生成可在 Android 设备上两个进程之间进行进程间通信(IPC)的代码。通过定义编辑 adil 文件,build 后生成对应的 java 类。

        如下,为定义的 MessageManager.aidl 文件经 build 后,生成的 MessageManager.java 接口的框架。该接口包含 sendMsg() 和 getMsg() 两个方法,以及一个名为 Stub 的静态抽象内部类;Stub 继承了 Binder,并实现了 MessageManager 接口,其内部定义了一个名为 Proxy 的静态内部类;Proxy 实现了 MessageManager 接口。

系统生成的 MessageManager 接口框架

        服务端:MessageManager.Stub mBind = new MessageManager.Stub()

        客户端:mMessageManager = MessageManager.Stub.asInterface(service)(service 是服务端传过来的Binder,即 mBind)

        本文全部代码见→使用AIDL实现进程间通讯简单案例(更新版)

2 项目结构

        注意: aidl_C 和 aidl_S 下的 com.zhyan8.aidl 包名及其中的 aidl 文件必须一致。

3 服务端 aidl_S 代码

        (1)创建 aidl 文件

        MessageManager.aidl

package com.zhyan8.aidl;

interface MessageManager {
    void sendMsg(String msg);
    String getMsg();
}

        注意:方法前不要添加 public 等修饰符;创建完 aidl 文件后,需要点击一下【Make Build】按钮,以生成对应的 Java 文件,否则 MyService 中导入 MessageManager 异常

        (2)创建服务

        MyService.java

package com.zhyan8.aidl_s;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.zhyan8.aidl.MessageManager;

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return mBind;
    }

    MessageManager.Stub mBind = new MessageManager.Stub() {
        @Override
        public void sendMsg(String msg) throws RemoteException {
            Log.d("MyService", "客户端发来消息: " + msg);
            System.out.println(msg);
        }

        @Override
        public String getMsg() throws RemoteException {
            return "abcde"; //客户端待接收的消息
        }
    };
}

        (3)注册服务

        在 AndroidManifest.xml 文件中 application 节点下注册 service,如下。

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.xxx.aidl"/>
    </intent-filter>
</service>

        (4)主 Activity

        MainActivity.java

package com.zhyan8.aidl_s;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

4 客户端 aidl_C 代码

        (1)复制 aidl 文件

        将 aidl_S 下的 com.zhyan8.aidl 包及其中的 aidl 文件复制到 aidl_C 中 。

        注意:创建完 aidl 文件后,需要点击一下【Make Build】按钮,以生成对应的 Java 文件,否则 MainActivity 中导入 MessageManager 异常

        (2)设计布局

        activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.zhyan8.aidl_c.MainActivity">

    <EditText
        android:id="@+id/et_msg"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:background="#ffcc66"/>

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="发送"
        android:textSize="30sp"
        android:layout_marginTop="30dp"/>

    <TextView
        android:id="@+id/tv_msg"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:background="#ffcc66"
        android:layout_marginTop="50dp"/>

    <Button
        android:id="@+id/btn_recv"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="接收"
        android:textSize="30sp"
        android:layout_marginTop="30dp"/>
</LinearLayout>

        界面如下:

        (3)主 Activity

        MainActivity.java

package com.zhyan8.aidl_c;

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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhyan8.aidl.MessageManager;

public class MainActivity extends AppCompatActivity {
    private MessageManager mMessageManager;
    private EditText et_msg;
    private Button btn_send;
    private TextView tv_msg;
    private Button btn_recv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public void init() {
        et_msg = (EditText) findViewById(R.id.et_msg);
        btn_send = (Button) findViewById(R.id.btn_send);
        tv_msg = (TextView) findViewById(R.id.tv_msg);
        btn_recv = (Button) findViewById(R.id.btn_recv);
        btn_send.setOnClickListener(cl);
        btn_recv.setOnClickListener(cl);
    }

    View.OnClickListener cl = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (v.getId()==R.id.btn_send) {
                String str = et_msg.getText().toString();
                sendMsg(str);
            }else if(v.getId()==R.id.btn_recv) {
                String str = getMsg();
                tv_msg.setText(str);
            }
        }
    };

    private void sendMsg(String str){
        if (mMessageManager==null) {
            attemptToBindService();
        }
        try {
            mMessageManager.sendMsg(str);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private String getMsg(){
        if (mMessageManager==null) {
            attemptToBindService();
        }
        try {
            String str = mMessageManager.getMsg();
            return str;
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void attemptToBindService() {
        Intent intent = new Intent();
        intent.setAction("com.xxx.aidl");
        intent.setPackage("com.zhyan8.aidl_s");
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMessageManager = MessageManager.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mMessageManager = null;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        if (mMessageManager==null) {
            attemptToBindService();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mMessageManager!=null) {
            unbindService(conn);
        }
    }
}

        注意:intent.setPackage("com.zhyan8.aidl_s") 中的 "com.zhyan8.aidl_s" 应与服务端 aidl_S 的 build.gradle 中 applicationId 属性的值一致,如下:

        (4)添加访问的包名

        Android 11 更改了应用查询用户已在设备上安装的其他应用以及与之交互的方式。使用新的 <queries> 标签,应用可以定义一组自身可访问的其他应用。在 AndroidManifest.xml 文件中 manifest 节点下面添加 queries 标签,如下

<queries>
    <package android:name="com.zhyan8.aidl_s" />
</queries>

5 效果展示

        (1)发送消息

        在 EditView 中输入【Qwert】,点击【发送】按钮,在服务端可以收到发送的消息,如下:

        (2)接收消息

        点击【接收】按钮,客户端 aidl_C 界面可以看到服务端 aidl_S 传过来的字符串【abcde】,如下:

6 附件

        以下是点击【Make Build】后自动生成的代码,路径为【aild_C\build\generated\source\aidl\debug\commu\ 】

        MessageManager.java

package com.zhyan8.aidl;

public interface MessageManager extends android.os.IInterface {

    public static class Default implements com.zhyan8.aidl.MessageManager {
        @Override
        public void sendMsg(java.lang.String msg) throws android.os.RemoteException {
        }

        @Override
        public java.lang.String getMsg() throws android.os.RemoteException {
            return null;
        }

        @Override
        public android.os.IBinder asBinder() {
            return null;
        }
    }

    public static abstract class Stub extends android.os.Binder implements com.zhyan8.aidl.MessageManager {
        private static final java.lang.String DESCRIPTOR = "com.zhyan8.aidl.MessageManager";

        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        public static com.zhyan8.aidl.MessageManager asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.zhyan8.aidl.MessageManager))) {
                return ((com.zhyan8.aidl.MessageManager) iin);
            }
            return new com.zhyan8.aidl.MessageManager.Stub.Proxy(obj);
        }

        @Override
        public android.os.IBinder asBinder() {
            return this;
        }

        @Override
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            java.lang.String descriptor = DESCRIPTOR;
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(descriptor);
                    return true;
                }
                case TRANSACTION_sendMsg: {
                    data.enforceInterface(descriptor);
                    java.lang.String _arg0;
                    _arg0 = data.readString();
                    this.sendMsg(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_getMsg: {
                    data.enforceInterface(descriptor);
                    java.lang.String _result = this.getMsg();
                    reply.writeNoException();
                    reply.writeString(_result);
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }
        }

        private static class Proxy implements com.zhyan8.aidl.MessageManager {
            private android.os.IBinder mRemote;

            Proxy(android.os.IBinder remote) {
                mRemote = remote;
            }

            @Override
            public android.os.IBinder asBinder() {
                return mRemote;
            }

            public java.lang.String getInterfaceDescriptor() {
                return DESCRIPTOR;
            }

            @Override
            public void sendMsg(java.lang.String msg) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    _data.writeString(msg);
                    boolean _status = mRemote.transact(Stub.TRANSACTION_sendMsg, _data, _reply, 0);
                    if (!_status && getDefaultImpl() != null) {
                        getDefaultImpl().sendMsg(msg);
                        return;
                    }
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }

            @Override
            public java.lang.String getMsg() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                java.lang.String _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    boolean _status = mRemote.transact(Stub.TRANSACTION_getMsg, _data, _reply, 0);
                    if (!_status && getDefaultImpl() != null) {
                        return getDefaultImpl().getMsg();
                    }
                    _reply.readException();
                    _result = _reply.readString();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            public static com.zhyan8.aidl.MessageManager sDefaultImpl;
        }

        static final int TRANSACTION_sendMsg = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_getMsg = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);

        public static boolean setDefaultImpl(com.zhyan8.aidl.MessageManager impl) {
            if (Stub.Proxy.sDefaultImpl == null && impl != null) {
                Stub.Proxy.sDefaultImpl = impl;
                return true;
            }
            return false;
        }

        public static com.zhyan8.aidl.MessageManager getDefaultImpl() {
            return Stub.Proxy.sDefaultImpl;
        }
    }

    public void sendMsg(java.lang.String msg) throws android.os.RemoteException;

    public java.lang.String getMsg() throws android.os.RemoteException;
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐