android开发aidl使用

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/wangjiangjun0815/article/details/79956852

最近写了一个aidl,发现有很多忘记了,感觉还是好记性不如烂笔头呀.特此记录下以便以后使用参考

一,创建service和client端,可以是二个不同的APP,也可以在同一个应用中使用二个进程.本文通过创建二个应用实现.aidl一般int ,long , boolean , float ,double, String ,和序列化对象

1,服务端创建序列化对象实现序列号接口

public class Book implements Parcelable{
    private String name;
    private String price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public static Creator<Book> getCREATOR() {
        return CREATOR;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.price);
    }

    public Book() {
    }

    protected Book(Parcel in) {
        this.name = in.readString();
        this.price = in.readString();
    }

    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel source) {
            return new Book(source);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    public void readFromParcel(Parcel reply) {

    }
}

2,创建同序列号相同的aidl类,包名必须相同否则会报无法找到序列号对象

// Book.aidl
package com.learn.www.service;

// Declare any non-default types here with import statements
parcelable Book;

3,创建序列号管理接口,导入序列化对象包名

// IBookManager.aidl
package com.learn.www.service;

// Declare any non-default types here with import statements
import com.learn.www.service.Book;
interface IBookManager {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
     List<Book> getBookItem();
     void addBook(inout Book book);//in 表示客户端输出,out表示服务端输出,inout表示服务端最终代替客户端输出

}

4,服务端创建service并清单文件中配置

public class MyService extends Service {
    private static List<Book> list=new ArrayList<>();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    static class MyBinder extends IBookManager.Stub{

        @Override
        public List<Book> getBookItem() throws RemoteException {
            Book book = new Book();
            book.setName("服务端传输");
            book.setPrice("服务端价格");
            list.add(book);
            return list;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            String name = book.getName();
            String price = book.getPrice();
            Log.e("==============","客户端"+name+price);
        }
    }
}

清单文件配置
 <service android:name=".MyService">
            <intent-filter >
                <action android:name="com.learn.www.service.aidl"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

二,1,客户端直接拷贝服务端aidl文件


2,开启aidl实现通信

 Intent intent = new Intent();
        intent.setPackage("com.learn.www.service");
        intent.setAction("com.learn.www.service.aidl");
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mstub = IBookManager.Stub.asInterface(service);
                Log.e("==============","开始可以通信");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        },BIND_AUTO_CREATE);
    }
    //获取服务端传递过来的信息
    public void get(View view) {
        try {
           List<com.learn.www.service.Book> bookItem = mstub.getBookItem();
            Book book = bookItem.get(0);
            Log.e("==============",book.getName());
            Log.e("==============",book.getPrice());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

            
阅读更多

没有更多推荐了,返回首页