在Android系统中实现AIDL 自定义对象传递

2024-09-05 07:58

本文主要是介绍在Android系统中实现AIDL 自定义对象传递,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  今天要在《在Android系统中实现AIDL接口回调》这篇文章的基础上实现AIDL自定义对象的传递功能。还是上一篇说到的三个项目:

├── SimpleJar
├── SimpleJarClient
└── SimpleJarService

一、在SimpleJar项目中添加aidl中要传递的对象StudentInfo.aidl跟StudentInfo.java,具体如下:

 ├── Android.mk
└── src
    └── com
        └── china
            └── jar
                ├── IVoiceCallBackInterface.aidl
                ├── IVoiceClientInterface.aidl
                ├── StudentInfo.aidl
                ├── StudentInfo.java
                ├── VoiceChangedListener.java
                └── VoiceManager.java
StudentInfo.aidl跟StudentInfo.java的代码实现如下:

package com.china.jar;
parcelable StudentInfo;
package com.china.jar;import android.os.Parcel;
import android.os.Parcelable;public class StudentInfo implements Parcelable{String name;int age;protected StudentInfo(Parcel in){name = in.readString();age = in.readInt();}public StudentInfo(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(age);}public static final Creator<StudentInfo> CREATOR = new Creator<StudentInfo>() {@Overridepublic StudentInfo createFromParcel(Parcel in) {return new StudentInfo(in);}@Overridepublic StudentInfo[] newArray(int size) {return new StudentInfo[size];}};
}

 由于是跨进程之间的通信,所以传递对象需要序列化,让StudentInfo实现Parcelable。这样就可以在IVoiceClientInterface.aidl中使用StudentInfo对象进行数据传递了,如下:

package com.china.jar;
import com.china.jar.IVoiceCallBackInterface;
import com.china.jar.StudentInfo;
interface IVoiceClientInterface{void face();void registerCallBack(IVoiceCallBackInterface callback);void unRegisterCallBack(IVoiceCallBackInterface callback);void registerUser(in StudentInfo studentInfo);
}

二、在SimpleJarService项目的SimpleService.java类中实现 registerUser(in StudentInfo studentInfo)方法,具体实现如下:

├── AndroidManifest.xml
├── Android.mk
├── libs
│  ├── android-support-v4.jar
│  ├── fw.jar
│  └── simple.jar
├── res
│  ├── drawable-hdpi
│  │  └── ic_launcher.png
│  ├── drawable-ldpi
│  ├── drawable-mdpi
│  │  └── ic_launcher.png
│  ├── drawable-xhdpi
│  │  └── ic_launcher.png
│  ├── layout
│  ├── values
│  │  ├── strings.xml
│  │  └── styles.xml
│  ├── values-v11
│  │  └── styles.xml
│  └── values-v14
│      └── styles.xml
└── src
    └── com
        └── china
            └── service
                ├── BootReceiverBroadcast.java
                ├── Logger.java
                ├── SimpleControl.java
                └── SimpleService.java
 

package com.china.service;import com.china.jar.IVoiceCallBackInterface;
import com.china.jar.IVoiceClientInterface;
import com.china.jar.StudentInfo;
import com.china.jar.VoiceChangedListener;
import com.china.jar.VoiceManager;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ServiceManager;public class SimpleService extends Service{private static VoiceClientInterfaceImpl mBinder;@Overridepublic IBinder onBind(Intent intent) {Logger.d();return mBinder;//跟客户端绑定}@Overridepublic void onCreate() {super.onCreate();Logger.d();if (null == mBinder){initService();}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Logger.d();if (null == mBinder){initService();}return START_STICKY;}//实现AIDL的接口private class VoiceClientInterfaceImpl extends IVoiceClientInterface.Stub{protected RemoteCallbackList<IVoiceCallBackInterface> mRemoteCallbackList = new RemoteCallbackList<IVoiceCallBackInterface>();private SimpleControl control;public VoiceClientInterfaceImpl(){control = new SimpleControl(voiceChangedListener);}@Overridepublic void face() throws RemoteException {Logger.d("face----excute!");//客户端调用face方法时这里会执行,会打印face----excute!}//注册回调@Overridepublic void registerCallBack(IVoiceCallBackInterface arg0)throws RemoteException {Logger.d();mRemoteCallbackList.register(arg0);}//注销回调@Overridepublic void unRegisterCallBack(IVoiceCallBackInterface arg0)throws RemoteException {Logger.d();mRemoteCallbackList.unregister(arg0);}//调用回调方法private VoiceChangedListener voiceChangedListener = new VoiceChangedListener() {@Overridepublic void openAppByVoice(String arg0) {Logger.d("arg0 = " + arg0);int len = mRemoteCallbackList.beginBroadcast();for (int i = 0; i <len; i++) {try {mRemoteCallbackList.getBroadcastItem(i).openAppByVoice(arg0);} catch (RemoteException e) {e.printStackTrace();}}mRemoteCallbackList.finishBroadcast();}};@Overridepublic void registerUser(StudentInfo studentInfo) throws RemoteException {Logger.d("name = " + studentInfo.getName() + " ,age = " + studentInfo.getAge());}}//初始化服务,主要是向系统注册服务private void initService(){Logger.d();if (null == mBinder){synchronized (SimpleService.class) {if (null == mBinder){try {mBinder = new VoiceClientInterfaceImpl();ServiceManager.addService(VoiceManager.NAME, mBinder);} catch (Exception e) {e.printStackTrace();}}}}}
}

 registerUser(in StudentInfo studentInfo)方法体中实现非常简单,就是打印学生姓名跟年龄。

三、在SimpleJarClient项目中调用registerUser(in StudentInfo studentInfo)方法,进行自定义对象的数据传递,如下:

 ├── AndroidManifest.xml
├── Android.mk
├── libs
│  └── simple.jar
├── res
│  ├── drawable-hdpi
│  │  └── ic_launcher.png
│  ├── drawable-ldpi
│  ├── drawable-mdpi
│  │  └── ic_launcher.png
│  ├── drawable-xhdpi
│  │  └── ic_launcher.png
│  ├── drawable-xxhdpi
│  │  └── ic_launcher.png
│  ├── layout
│  │  ├── activity_main.xml
│  │  ├── activity_tss.xml
│  │  ├── test.xml
│  │  ├── usb.xml
│  │  └── version.xml
│  ├── menu
│  ├── values
│  │  ├── dimens.xml
│  │  └── strings.xml
│  ├── values-v11
│  ├── values-v14
│  └── values-w820dp
│      └── dimens.xml
└── src
    └── com
        └── example
            └── helloworld
                ├── TestVoice.java
                ├── UsbTest.java
                └── util
                    ├── Logger.java
                    └── USBUtil.java
 

package com.example.helloworld;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;import com.china.jar.StudentInfo;
import com.china.jar.VoiceChangedListener;
import com.china.jar.VoiceManager;
import com.example.helloworld.util.Logger;public class TestVoice extends Activity{VoiceManager voiceManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.version);voiceManager = VoiceManager.getInstance();if (voiceManager != null){voiceManager.addVoiceChangedListener(new VoiceChangedListener() {//客户端的监听及实现@Overridepublic void openAppByVoice(String arg0) {Logger.d("packageName = " + arg0);}});}voiceManager.registerUser(new StudentInfo("赵敏", 18));//aidl中客户端传递自定义对象StudentInfo到服务端}public void startVoice(View view){Logger.d();Intent intent = new Intent();intent.setClass(TestVoice.this, UsbTest.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);}public void stopVoice(View view){Logger.d();VoiceManager.getInstance().face();}public void finishVoice(View view){Logger.d();finish();}
}

四、运行结果如下:

 

代码参考路径:https://github.com/gunder1129/android-tool/tree/master/AIDLdemo

 

 

 

 

 

这篇关于在Android系统中实现AIDL 自定义对象传递的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138379

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序