在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+PyQt5实现MySQL数据库备份神器

《Python+PyQt5实现MySQL数据库备份神器》在数据库管理工作中,定期备份是确保数据安全的重要措施,本文将介绍如何使用Python+PyQt5开发一个高颜值,多功能的MySQL数据库备份工具... 目录概述功能特性核心功能矩阵特色功能界面展示主界面设计动态效果演示使用教程环境准备操作流程代码深度解

golang float和科学计数法转字符串的实现方式

《golangfloat和科学计数法转字符串的实现方式》:本文主要介绍golangfloat和科学计数法转字符串的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望... 目录golang float和科学计数法转字符串需要对float转字符串做处理总结golang float

linux lvm快照的正确mount挂载实现方式

《linuxlvm快照的正确mount挂载实现方式》:本文主要介绍linuxlvm快照的正确mount挂载实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux lvm快照的正确mount挂载1. 检查快照是否正确创建www.chinasem.cn2.

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

使用Python和Tkinter实现html标签去除工具

《使用Python和Tkinter实现html标签去除工具》本文介绍用Python和Tkinter开发的HTML标签去除工具,支持去除HTML标签、转义实体并输出纯文本,提供图形界面操作及复制功能,需... 目录html 标签去除工具功能介绍创作过程1. 技术选型2. 核心实现逻辑3. 用户体验增强如何运行

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

Python实现文件批量重命名器

《Python实现文件批量重命名器》在日常工作和学习中,我们经常需要对大量文件进行重命名操作,本文将介绍一个使用Python开发的文件批量重命名工具,提供了多种重命名模式,有需要的小伙伴可以了解下... 目录前言功能特点模块化设计1.目录路径获取模块2.文件列表获取模块3.重命名模式选择模块4.序列号参数配

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

SpringBoot实现多环境配置文件切换

《SpringBoot实现多环境配置文件切换》这篇文章主要为大家详细介绍了如何使用SpringBoot实现多环境配置文件切换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 示例代码结构2. pom文件3. application文件4. application-dev文