【Android】使用AIDL实现进程间传递对象案例
1 前言
在 Android——使用AIDL实现进程间通讯简单案例 中介绍了使用 AIDL 在进程间传递字符串,对于8种基本数据类型( byte、short、int、long、float、double、boolean、char )和 CharSequence(包含 String )、List、Map,用法同理。
需要注意:List 和 Map 中的所有元素必须是 AIDL 支持的类型,List 支持泛型,Map 不支持泛型。
本文将介绍使用 AIDL 实现自定义 User 对象间的传递,User 包含 name(String)和 age(int)2个属性。
本文全部代码见→使用AIDL实现进程间传递对象案例
2 项目结构

注意事项:
- User.java 和 User.aidl 的包名必须一致
- aidl_C 和 aidl_S 下的 User.java 文件内容必须一致
- aidl_C 和 aidl_S 下的 aidl 文件及其包名必须一致
3 服务端 aidl_s 代码
(1)传输类定义
User.java
package commu;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable{
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
protected User(Parcel in) {
name = in.readString();
age = in.readInt();
}
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];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
(2)传输类声明
User.aidl
package commu;
parcelable User;
注意: 声明 parcelable,不是 Parcelable
(3)通讯接口
MessageManager.aidl
package commu;
import commu.User;
interface MessageManager {
void sendMsg(in User user);
User getMsg();
}
(4)服务
MyService.java
package com.zhyan8.aidl_s;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import commu.MessageManager;
import commu.User;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return mBind;
}
MessageManager.Stub mBind = new MessageManager.Stub() {
@Override
public void sendMsg(User user) throws RemoteException {
Log.d("MyService", "客户端发来消息: " + user.toString());
System.out.println(user.toString());
}
@Override
public User getMsg() throws RemoteException {
return new User("小红",23); //客户端待接收的消息
}
};
}
(5)注册服务
在 AndroidManifest.xml 文件中 application 节点下注册 service,如下。
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.xxx.aidl"/>
</intent-filter>
</service>
(6)主Acitvity
MainActivity.java
package com.zhyan8.aidl_s;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4 客户端 aidl_c 代码
(1)复制 User 类及 aidl 文件
将 aidl_S 中 java 和 aidl 目录下的 commu 包及其中的 aidl 文件(User.aidl、MessageManager.aidl) 和 User.java 文件复制到 aidl_C 中相应目录下,见第2节中项目结构图 。
(2)布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.zhyan8.aidl_c.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="30sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="30sp"
android:background="#ffcc66"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="30sp"/>
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="30sp"
android:inputType="number"
android:background="#ffcc66"/>
</LinearLayout>
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="发送"
android:textSize="30sp"
android:layout_marginTop="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="30sp"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="30sp"
android:background="#ffcc66"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="30sp"/>
<TextView
android:id="@+id/tv_age"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="30sp"
android:background="#ffcc66"/>
</LinearLayout>
<Button
android:id="@+id/btn_recv"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="接收"
android:textSize="30sp"
android:layout_marginTop="30dp"/>
</LinearLayout>
界面如下:

(3)主Activity
MainActivity.java
package com.zhyan8.aidl_c;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import commu.MessageManager;
import commu.User;
public class MainActivity extends AppCompatActivity {
private MessageManager mMessageManager;
private EditText et_name;
private EditText et_age;
private Button btn_send;
private TextView tv_name;
private TextView tv_age;
private Button btn_recv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
et_name = (EditText) findViewById(R.id.et_name);
et_age = (EditText) findViewById(R.id.et_age);
btn_send = (Button) findViewById(R.id.btn_send);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_age = (TextView) findViewById(R.id.tv_age);
btn_recv = (Button) findViewById(R.id.btn_recv);
btn_send.setOnClickListener(cl);
btn_recv.setOnClickListener(cl);
}
View.OnClickListener cl = new View.OnClickListener(){
@Override
public void onClick(View v) {
hideInputMethod(MainActivity.this, v); //关闭输入法
if (v.getId()==R.id.btn_send) {
try {
String name_t = et_name.getText().toString();
int age_t = Integer.parseInt(et_age.getText().toString());
User user = new User(name_t, age_t);
sendMsg(user);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "请输入姓名和年龄", Toast.LENGTH_SHORT).show();
}
}else if(v.getId()==R.id.btn_recv) {
User user = getMsg();
tv_name.setText(user.getName());
tv_age.setText("" + user.getAge());
}
}
};
private void sendMsg(User user){
if (mMessageManager==null) {
attemptToBindService();
}
try {
mMessageManager.sendMsg(user);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private User getMsg(){
if (mMessageManager==null) {
attemptToBindService();
}
try {
User user = mMessageManager.getMsg();
return user;
} catch (RemoteException e) {
e.printStackTrace();
}
return null;
}
private void attemptToBindService() {
Intent intent = new Intent();
intent.setAction("com.xxx.aidl");
intent.setPackage("com.zhyan8.aidl_s");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMessageManager = MessageManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mMessageManager = null;
}
};
private void hideInputMethod(Activity act, View v) { //关闭输入法
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
@Override
protected void onStart() {
super.onStart();
if (mMessageManager==null) {
attemptToBindService();
}
}
@Override
protected void onStop() {
super.onStop();
if (mMessageManager!=null) {
unbindService(conn);
}
}
}
5 效果展示
(1)发送消息
在2个 EditView 中分别输入::小明、20,点击【发送】按钮,在服务端可以收到发送的消息,如下。

(2)接收消息
点击【接收】按钮,客户端 aidl_C 界面可以看到服务端 aidl_S 传过来的 user 信息,如下。

6 附件
以下是点击【Make Build】后自动生成的代码,路径为【aild_C\build\generated\source\aidl\debug\commu\ 】
MessageManager.java
package commu;
public interface MessageManager extends android.os.IInterface {
public static class Default implements commu.MessageManager {
@Override
public void sendMsg(commu.User user) throws android.os.RemoteException {
}
@Override
public commu.User getMsg() throws android.os.RemoteException {
return null;
}
@Override
public android.os.IBinder asBinder() {
return null;
}
}
public static abstract class Stub extends android.os.Binder implements commu.MessageManager {
private static final java.lang.String DESCRIPTOR = "commu.MessageManager";
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
public static commu.MessageManager asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof commu.MessageManager))) {
return ((commu.MessageManager) iin);
}
return new commu.MessageManager.Stub.Proxy(obj);
}
@Override
public android.os.IBinder asBinder() {
return this;
}
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_sendMsg: {
data.enforceInterface(descriptor);
commu.User _arg0;
if ((0 != data.readInt())) {
_arg0 = commu.User.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.sendMsg(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getMsg: {
data.enforceInterface(descriptor);
commu.User _result = this.getMsg();
reply.writeNoException();
if ((_result != null)) {
reply.writeInt(1);
_result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} else {
reply.writeInt(0);
}
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements commu.MessageManager {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
@Override
public android.os.IBinder asBinder() {
return mRemote;
}
public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}
@Override
public void sendMsg(commu.User user) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((user != null)) {
_data.writeInt(1);
user.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
boolean _status = mRemote.transact(Stub.TRANSACTION_sendMsg, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().sendMsg(user);
return;
}
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public commu.User getMsg() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
commu.User _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_getMsg, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
return getDefaultImpl().getMsg();
}
_reply.readException();
if ((0 != _reply.readInt())) {
_result = commu.User.CREATOR.createFromParcel(_reply);
} else {
_result = null;
}
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
public static commu.MessageManager sDefaultImpl;
}
static final int TRANSACTION_sendMsg = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getMsg = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
public static boolean setDefaultImpl(commu.MessageManager impl) {
if (Stub.Proxy.sDefaultImpl == null && impl != null) {
Stub.Proxy.sDefaultImpl = impl;
return true;
}
return false;
}
public static commu.MessageManager getDefaultImpl() {
return Stub.Proxy.sDefaultImpl;
}
}
public void sendMsg(commu.User user) throws android.os.RemoteException;
public commu.User getMsg() throws android.os.RemoteException;
}
声明:本文转自【Android】使用AIDL实现进程间传递对象案例
【Android】使用AIDL实现进程间传递对象案例的更多相关文章
- Android 使用AIDL实现进程间的通信
在Android中,如果我们需要在不同进程间实现通信,就需要用到AIDL技术去完成. AIDL(android Interface Definition Language)是一种接口定义语言,编译器通 ...
- 在Android中通过Intent使用Bundle传递对象
IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数
一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- IBinder对象在进程间传递的形式(一)
命题 当service经常被远程调用时,我们经常常使用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind servic ...
- C#中使用SendMessage在进程间传递数据的实例
原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...
- 进程间传递文件描述符——sendmsg和recvmsg函数
先引入一个例子,该程序的目的是子进程向父进程传递文件描述符,并通过该文件描述符读取buf. #include <func.h> int main(){ int fds[2]; pipe(f ...
- WPF 进程间传递参数
WPF 进程间传递参数 在软件开发中有时需要在一个软件中启动另一个软件,这时用Process.Start(“软件路径”)可以启动另一个软件.如果在这个过程中还需要传递一些参数给新启动 ...
- 进程间传递文件描述符fd
众所周知,子进程会继承父进程已经打开的文件描述符fd,但是fork之后的是不会被继承的,这个时候是否无能无力了?答应是NO.Linux提供了一个系统调用sendmsg,借助它,可以实现进程间传递文件描 ...
- Linux 进程间传递文件描述符
文章目录 文件描述符 文件数据结构 共享文件 UNIX域socket实现传递文件描述符 进程间传递打开的文件描述符,并不是传递文件描述符的值.先说一下文件描述符. 文件描述符 对内核来说,所有打开的文 ...
随机推荐
- 如何让你的.NET WebAPI程序支持HTTP3?
下面我将总结构建Http3的经验,以Token Gateway的项目为例,请注意使用Http3之前你需要知道它的限制, Windows Windows 11 版本 22000 或更高版本/Window ...
- tiup 工具离线安装与简单导出数据说明
tiup 工具离线安装说明 mirror的创建 能上网的机器上面进行如下操作: curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pi ...
- [转帖]关于kafka压力测试(使用官方自带脚本测试)
文章目录 kafka官方自带压测脚本文件 Producer生产者环境测试 测试命令 返回测试结果 Consumer消费者环境测试 测试命令 测试结果说明 提升kafka的吞吐量 可通过以下的方式来提升 ...
- Linux执行SQLSERVER语句的简单方法
背景 因为WTF的原因.经常有人让执行各种乱七八槽的删除语句 因为产品支持了10多种数据库. 这个工作量非常复杂. 为了简单起见,想着能够批量执行部分SQL. 其他的都处理过了,但是SQLSERVER ...
- UOS可能的来源
1050a 行业版 是基于 阿里的Anolis 1050d 企业版 是基于debian 1050e 欧拉版 是基于华为欧拉 euler
- python批量上传文件到七牛云
导航 引子 棘手的需求 化繁为简 实战案例 结语 参考 本文首发于智客工坊-<python批量上传文件到七牛云>,感谢您的阅读,预计阅读时长3min. 古之立大事者,不惟有超世之才,亦必有 ...
- Vue3中hook的简单使用
创建文件夹 在src下创建文件夹.文件名称为hooks. hooks下的文件夹下,是你的封装的hook: 通过命名为useXXXXXX usexy.js 文件是封装的获取屏幕的坐标 import { ...
- 菜鸟教程-所有软件教学都有【python、java、c、c++、html、sql、css、jquery、bootstrap、vue、c#、go】
软件教学[python.java.c.c++.html.sql.css.jquery.bootstrap.vue.c#.go] 首页 https://www.runoob.com/ 资料很全 1.p ...
- 2.1 C/C++ 使用数组与指针
C/C++语言是一种通用的编程语言,具有高效.灵活和可移植等特点.C语言主要用于系统编程,如操作系统.编译器.数据库等:C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统.图形用户界面 ...
- 《重学Java设计模式》作者开始录视频了!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 1. 前言 哈哈哈,终于对B站下手了! 大家好,我是小傅哥,在紧张.羞涩到适应后,哈哈哈,终于 ...