美文网首页
Aidl初步使用教程

Aidl初步使用教程

作者: ahah95 | 来源:发表于2018-11-21 18:00 被阅读0次
image.png

一、创建AIDL文件

image.png

Aidl的包名是自动生成的与项目的包名一致

IMyAidlInterface.aidl

package com.master.mydagger2;

// Declare any non-default types here with import statements
//import com.master.mydagger2.User;
import com.master.mydagger2.User;


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

    String getName();

    User getUser();
}

创建好后重新编译下会自动生成IMyAidlInterface的动态代理类

image.png

二、创建Service并暴露(另一个App可以通过Binder通道得到Service)

image.png

MyService

package com.master.mydagger2.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.master.mydagger2.IMyAidlInterface;
import com.master.mydagger2.User;


public class MyService extends Service {

    public MyService() {
    }

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

    class MyBinder extends IMyAidlInterface.Stub {

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

        }

        //aidl  getName()接口的具体实现
        @Override
        public String getName() throws RemoteException {
            return "name";
        }

        //aidl  getUser()接口的具体实现
        @Override
        public User getUser() throws RemoteException {
            return new User();
        }


    }
}

三、将aidl整个文件夹复制到新的项目下

image.png

包名必须一致

四、调用aidl

package com.master.mydagger2aidl;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.master.mydagger2.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    private IMyAidlInterface iMyAidlInterface;

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

        //android5.0之后无法通过隐式Intent绑定Service
        Intent it= new Intent();
        it.setPackage("com.master.mydagger2");//包名必须与目标项目一直
        it.setAction("com.master.mydagger2.action");

        bindService(it, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    System.out.println("connected    ------" + iMyAidlInterface.getUser());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);


//        try {
//            Toast.makeText(MainActivity.this, iMyAidlInterface.getName(), Toast.LENGTH_SHORT).show();
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }

    }
}

关于Aidl的传值

一、创建Bean类

User

package com.master.mydagger2;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {

    private String name = "UserName";

    public User() {

    }
    //Parcelable自动生成
    protected User(Parcel in) {
        name = in.readString();
    }
    //Parcelable自动生成
    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
    //Parcelable自动生成   默认返回0即可
    @Override
    public int describeContents() {
        return 0;
    }
    //Parcelable自动生成  写入值到Parcel
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

User类必须放在包的根目录下(因为需要与aidl映射,aidl接口只能在包根目录下)

image.png

二、创建Aidl

// User.aidl
package com.master.mydagger2;

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

parcelable User;

image.png

三、在IMyAidlInterface中引用User.aidl

// IMyAidlInterface.aidl
package com.master.mydagger2;

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

import com.master.mydagger2.User;//User.aidl


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

    String getName();

    User getUser();
}

四、在MyService中添加getUser()接口的实现

package com.master.mydagger2.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.master.mydagger2.IMyAidlInterface;
import com.master.mydagger2.User;


public class MyService extends Service {

    public MyService() {
    }

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

    class MyBinder extends IMyAidlInterface.Stub {

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

        }

        //aidl  getName()接口的具体实现
        @Override
        public String getName() throws RemoteException {
            return "name";
        }

        //aidl  getUser()接口的具体实现
        @Override
        public User getUser() throws RemoteException {
            return new User();
        }


    }
}

五、在另一个项目的aidl下添加User.aidl

image.png

操作同上

六、调用

image.png

相关文章

网友评论

      本文标题:Aidl初步使用教程

      本文链接:https://www.haomeiwen.com/subject/geocqqtx.html