365bet官方贴吧-365bet官网备用网站-365限制投注额度怎么办

Android:如何创建一个AIDL

Android:如何创建一个AIDL

注意:本篇文章是本人阅读相关文章所写下的总结,方便以后查阅,所有内容非原创,侵权删。

本篇文章内容来自于:

1.Android开发艺术探索

2.Android 进阶7:进程通信之 AIDL 的使用

目录

第一步:创建AIDL

1.1 创建要操作的实体类

1.2 新建 aidl 文件夹

1.3 Make project

第二步:创建服务端

第三步:创建客户端

1. 第一步:创建AIDL

1.1 创建要操作的实体类,实现 Parcelable 接口,以便序列化/反序列化 Book.class

public class Book implements Parcelable {

int bookId;

String bookName;

public Book(int bookId, String bookName) {

this.bookId = bookId;

this.bookName = bookName;

}

protected Book(Parcel in) {

bookId = in.readInt();

bookName = in.readString();

}

public static final Creator CREATOR = new Creator() {

@Override

public Book createFromParcel(Parcel in) {

return new Book(in);

}

@Override

public Book[] newArray(int size) {

return new Book[size];

}

};

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeInt(bookId);

dest.writeString(bookName);

}

}

1.2 新建 aidl 文件夹,在其中创建接口 aidl 文件以及实体类的映射 aidl 文件

创建实体类的映射 aidl 文件,Book.aidl:

// Book.aidl

package com.example.apple.encryptiondemo;

//还要和声明的实体类在一个包里

parcelable Book;

接口 aidl 文件:

package com.example.apple.encryptiondemo;

import com.example.apple.encryptiondemo.Book;

interface IBookManager {

List getBookList();

void addBook(in Book book);

}

1.3 Make project ,生成 Binder 的 Java 文件

在 build/generated/source/aidl/你的 flavor/ 下生成一个 Java 文件

2. 第二步:创建服务端

创建 Service,在其中创建上面生成的 Binder 对象实例,实现接口定义的方法

在 onBind() 中返回

public class BookService extends Service {

private final String TAG = this.getClass().getSimpleName();

private ArrayList mBooks;

private IBinder iBinder = new IBookManager.Stub() {

@Override

public List getBookList() throws RemoteException {

return mBooks;

}

@Override

public void addBook(Book book) throws RemoteException {

mBooks.add(book);

}

};

@Nullable

@Override

public IBinder onBind(Intent intent) {

mBooks = new ArrayList<>();

return iBinder;

}

}

3. 第三步:创建客户端

实现 ServiceConnection 接口,在其中拿到 AIDL 类

bindService()

调用 AIDL 类中定义好的操作请求

public class MainActivity extends BaseActivity {

@BindView(R.id.btn1)

Button btn1;

@BindView(R.id.tv_show)

TextView tvShow;

private IBookManager iBookManager;

private ServiceConnection connection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

iBookManager = IBookManager.Stub.asInterface(service);

}

@Override

public void onServiceDisconnected(ComponentName name) {

iBookManager = null;

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

Intent intent = new Intent(this, BookService.class);

bindService(intent, connection, BIND_AUTO_CREATE);

}

@OnClick({R.id.btn1})

public void onViewClicked(View view) {

switch (view.getId()) {

case R.id.btn1:

Random random = new Random();

int id = random.nextInt(100);

Book book = new Book(id, "book" + id);

try {

iBookManager.addBook(book);

List bookList = iBookManager.getBookList();

tvShow.setText(bookList.toString());

} catch (RemoteException e) {

e.printStackTrace();

}

break;

}

}

}

← 上一篇: C#使用OpenCvSharp4库中5个基础函数
下一篇: 2025年米粉卡流量包全解析:套餐选择与使用攻略 →

相关推荐

世界杯奖图片

世界杯奖图片

2025-09-05 23:59:22 阅读: 4863
大叔, 哪里逃!更新至418章精彩大结局

大叔, 哪里逃!更新至418章精彩大结局

2025-09-02 23:34:13 阅读: 6169
海信电视怎么连接机顶盒

海信电视怎么连接机顶盒

2025-10-09 09:35:57 阅读: 1798
海外代购如何报关

海外代购如何报关

2025-09-02 21:30:02 阅读: 4046
Candy Crush

Candy Crush

2025-09-14 07:59:17 阅读: 7496
揭秘:国内热门娱乐八卦网站排行榜

揭秘:国内热门娱乐八卦网站排行榜

2025-08-26 08:16:40 阅读: 3845
避孕震动安全套

避孕震动安全套

2025-09-12 13:12:10 阅读: 3817