AIDL的全称是什么?如何工作?

android interface difine language(android自定义接口语言)
作用:进程间通信
当A进程要去调用B进程中的service时,并实现通信,我们通常都是通过AIDL来操作的A工程
工作:
期望结果:Activity需要绑定一个服务,并且回调Aidl接口实现方法
步骤:
1.在Activity中编写一个绑定方法,并且实例化相应的回调方法

private void bind() {
        Intent in = new Intent(this, MyBoundService.class);
        //异步绑定,如果是绑定成功后会回调onServiceConnected
        bindService(in, con, Context.BIND_AUTO_CREATE);//没有创建就自动创建,创建了就直接绑定
    }
//绑定服务的连接回调接口
    private ServiceConnection con = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //绑定成功调用
            cat = ICat.Stub.asInterface(service);
            Toast.makeText(ServiceActivity.this, cat + "", Toast.LENGTH_SHORT).show();
            bound = true;
            Toast.makeText(ServiceActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //服务异常调用(正常退出不会调用的)
            bound = false;
        }
    };

2.编写MyBoundService.java类

public class MyBoundService extends Service {
@Override
    public IBinder onBind(Intent intent) {
        return new CatImpl();
    }
}

3.编写CatImpl.java类(Aidl开始编写)

首先:

public class CatImpl extends ICat.Stub

ICat.Stub又是什么呢?这就涉及到Aidl文件的创建了

  1. 使用Android Studio右键new Aidl–>Aidl file ICat.aidl
  2. Android studio会创建在main/aidl/包名/ICat.aidl
// ICat.aidl
package com.recycler.zx.zxrecyclerview.Service;

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

interface ICat {
    /**
     * 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);
}

它是一个接口,你可以在上面添加自己定义的接口方法如下我创建了一个setName的方法:

// ICat .aidl
package com.recycler.zx.zxrecyclerview.Service;

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

interface ICat {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
     //自己定义的
     void setUserName(String userName);
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

Aidl文件编写好了,下面就是调用了
3. 当你写完Aidl文件后,执行build/Rebuild Project,重新build一下
android studio会自动生成一个针对你的aidl的java文件
路径:app/build/generated/source/aidl/debug/包名/IPpInterface.java

public interface ICat extends android.os.IInterface

不用关心这个文件,系统自动生成的
4. 接口写完了,该写具体接口实现方法了
(刚刚你编写的接口在你build以后已经“变成了”抽象方法了)

public class CatImpl extends ICat.Stub {
private String mUserName;
    @Override
    public void setUserName(String userName) throws RemoteException {
    //编写实现方法:
            this.mUserName = userName;
    }

    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

    }
}

到此,Aidl调用和编写就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆啦A梦z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值