android应用层aidl的简单使用

aidl的Android提供的方便我们进行应用间通信的工具,其全称是Android Interface define language,本文记录一下如何在两个应用之间通过aidl进行通信。和普通的接口文件相比有如下的一些特性

  • 只能定义方法,不能定义常量
  • 支持8中基本类型
  • 支持String,CharSequence
  • 支持List,确切的说只支持ArrayList
  • 支持Map(HashMap)
  • 支持Parcelable对象

1、接口定义

在项目中main目录下,创建一个aidl文件夹,再创建aidl文件。

// IMyAidlInterface.aidl
package com.hht.douyin;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    String getName();
    void setName(String name);
}

项目结构如下
在这里插入图片描述
定义完之后先build一遍项目,AS会自动生成相关的类,在build目录下可以看到。

2、服务端实现

这里我们定义一个Service,用来实现aidl中定义的方法。

public class MyService extends Service {
    private static final String TAG = "MyService";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    class MyBinder extends IMyAidlInterface.Stub{

        private String name = "张三";

        @Override
        public String getName() throws RemoteException {
            return name;
        }

        @Override
        public void setName(String name) throws RemoteException {
            this.name = name;
        }
    }
}

这里写了一个内部类继承IMyAidlInterface.Stub类,Stub类是AS根据我们定义的aidl文件生成的类,绑定服务后就可以获取到一个MyBinder对象。不要忘记将service在AndroidManifest文件中注册

        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true">

        </service>

3、客户端实现

先将我们之间定义的aidl文件复制过来,必须保持一模一样,最简单就是将aidl文件夹复制过来就行了。重新build一遍项目,然后绑定远程服务,和绑定本地服务几乎是一样的。

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Button buttonGet;
    private Button buttonSet;
    private Button buttonBind;

    private Intent bindIntent;

    private IMyAidlInterface binder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = IMyAidlInterface.Stub.asInterface(service);
            Log.d(TAG, "onServiceConnected: 绑定成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initData();
        buttonGet.setOnClickListener(v->{
            String name = null;
            try {
                name = binder.getName();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "onCreate: "+name);
        });
        buttonSet.setOnClickListener(v->{
            try {
                binder.setName("华为");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        });

    }

    private void initViews(){
        buttonGet = findViewById(R.id.button1);
        buttonSet = findViewById(R.id.button2);
        buttonBind = findViewById(R.id.button0);
    }

    private void initData(){
        buttonBind.setOnClickListener(v->{
            bindIntent = new Intent();
            ComponentName name = new ComponentName("com.hht.module1","com.hht.module1.MyService");
            bindIntent.setComponent(name);
            bindService(bindIntent,connection,BIND_AUTO_CREATE);
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}

这里没有什么好说的,绑定成功后在回调函数中将IBinder对象转换成IMyAidlInterface对象就行了。Android11以前的版本到这里就可以了,但是Android11之后还必须在客户端AndroidManifest文件中通过quries标签指明我们需要交互的程序,这是在新版本中更新的。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hht.douyin">

    <queries>
        <package android:name="com.hht.module1"/>
    </queries>
    <application
        android:name="com.hht.global.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Douyin">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

请记住这个quries标签,因为几乎涉及到和其他程序通信的都需要在这里声明,比如使用其他程序提供的ContentProvider。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值