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

版权声明:本文为博主原创文章,未经博主允许不得转载。若转载请注明出处 https://blog.csdn.net/qq_26761229/article/details/79470115
http://blog.csdn.net/zx_android/article/details/50715686

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文件的创建了

使用Android Studio右键new Aidl–>Aidl file ICat.aidl
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调用和编写就完成了。

猜你喜欢

转载自blog.csdn.net/qq_26761229/article/details/79470115