AIDL基本使用3—-in out inout的用

2024-03-10 19:59
文章标签 使用 基本 aidl inout

本文主要是介绍AIDL基本使用3—-in out inout的用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在AIDL中客户端和服务端传入参数 是可以设置流向.仅限参数不包含返回值
1. in :客户端可以传入参数到服务到(默认方法)
2. out:服务端修改客户端传入参数对象 会影响客户端的传入实例
3. inout:服务端即可接受客户端参数也可以修改对其客户端实例影响

这个标签在哪?


这里用AIDL基本使用2的Demo作为案例:AIDL基本使用2

在AIDL基本使用2案例中 IMyAidlInterface.aidl 用来作为客户端和服务端交互的接口.

// IMyAidlInterface.aidl
package com.ucoupon.myservice;
import com.ucoupon.myservice2.Book;interface IMyAidlInterface {String bookIn(in Book mbook);String bookOut(out Book mbook);String bookInout(inout Book mbook);
}

看上面的代码中可以知道.in out inout是用来修饰aidl接口中传入参数.

我们在复习下Book.java 里面有什么

package com.ucoupon.myservice2;import android.os.Parcel;
import android.os.Parcelable;/*** Created by FMY on 2017/5/18.*/public class Book implements Parcelable {String name;int id;public Book(String name, int id) {this.name = name;this.id = id;}protected Book(Parcel in) {name = in.readString();id = in.readInt();}public static final Creator<Book> CREATOR = new Creator<Book>() {@Overridepublic Book createFromParcel(Parcel in) {return new Book(in);}@Overridepublic Book[] newArray(int size) {return new Book[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(id);}
}

一个很普通的类有两个字段

  • 来编译看看
    编译出错,出错于自动生成接口java文件.new Book()无法被实例化.
    因为根本没有这个构造方法.
    这里写图片描述

为什么我们在AIDL基本使用的2中没有报错?
继续查看源码

这里写图片描述

如果被修饰方法参数为out那么会自动创建修饰参数的空构造方法
在本例中有如下方法被out修饰:
String bookOut(out Book mbook);

解决办法:创建一个空构造方法

  • 继续编译
    编译又报错
    这里写图片描述

可以看到Book实例化调用readFromParcel方法.可是book没有这个方法.

解决办法:
在Book类中添加此方法.此方法是用于客户端用了out或者inout修饰的方法 中读取服务端修改后的对象的数值在赋值给客户端

package com.ucoupon.myservice2;import android.os.Parcel;
import android.os.Parcelable;/*** Created by FMY on 2017/5/18.*/public class Book implements Parcelable {....public void readFromParcel(Parcel reply) {name = reply.readString();id = reply.readInt();}.....
}

修改后编译通过;

然后把文件拷贝到客户端 进行绑定操作 这一步略过


in测试

来看服务段代码

package com.ucoupon.myservice;import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;import com.ucoupon.myservice2.Book;public class MyService extends Service {public MyService() {}@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "onCreate() called");}//绑定的时候回调@Nullable@Overridepublic IBinder onBind(Intent intent) {Log.d(TAG, "onBind() called with: intent = [" + intent + "]");return new MyAidlInterface();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");return super.onStartCommand(intent, flags, startId);}@Overridepublic void unbindService(ServiceConnection conn) {Log.d(TAG, "unbindService() called with: conn = [" + conn + "]");super.unbindService(conn);}@Overridepublic void onRebind(Intent intent) {Log.d(TAG, "onRebind() called with: intent = [" + intent + "]");super.onRebind(intent);}@Overridepublic boolean onUnbind(Intent intent) {Log.d(TAG, "onUnbind() called with: intent = [" + intent + "]");return super.onUnbind(intent);}@Overridepublic void unregisterReceiver(BroadcastReceiver receiver) {Log.d(TAG, "unregisterReceiver() called with: receiver = [" + receiver + "]");super.unregisterReceiver(receiver);}@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy() called");}@Overridepublic void onStart(Intent intent, int startId) {Log.d(TAG, "onStart() called with: intent = [" + intent + "], startId = [" + startId + "]");super.onStart(intent, startId);}private static final String TAG = "MyService";class MyAidlInterface extends IMyAidlInterface.Stub{@Overridepublic String bookIn(Book mbook) throws RemoteException {Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}@Overridepublic String bookOut(Book mbook) throws RemoteException {Log.d(TAG, "bookout() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}@Overridepublic String bookInout(Book mbook) throws RemoteException {Log.d(TAG, "bookInout() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}}
}

客户端:

package com.ucoupon.aidlstudy;import android.content.ComponentName;
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.util.Log;import com.ucoupon.myservice.IMyAidlInterface;
import com.ucoupon.myservice2.Book;public class MainActivity extends AppCompatActivity {private Myconnect myconnect;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//连接管理myconnect = new Myconnect();//意图Intent intent = new Intent();intent.setClassName("com.ucoupon.myservice","com.ucoupon.myservice.MyService");startService(intent);//开始绑定服务bindService(intent,myconnect,BIND_AUTO_CREATE);}private static final String TAG = "MainActivity";class Myconnect implements ServiceConnection {//连接的成功的时候回调@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d(TAG, "onServiceConnected() called with: name = [" + name + "], service = [" + service + "]");IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);Book mBook = new Book("客户端", -1);try {Log.e("fmy","客户端没有调用 bookin方法前mBook: name = "+mBook.name+","+" id"+mBook.id);iMyAidlInterface.bookIn(mBook);Log.e("fmy","客户端调用后 bookin方法后mBook: name = "+mBook.name+","+" id"+mBook.id);} catch (RemoteException e) {e.printStackTrace();}//解绑服务
//            unbindService(myconnect);}//断开连接的时候回调@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d(TAG, "onServiceDisconnected() called with: name = [" + name + "]");}}
}

结果预测:
根据前言in 可以让客户端传入一个对象给服务端.但是服务端拿到对象后修改对客户端实例是没有效果

运行结果:
客户端:

E/fmy: 客户端没有调用 bookin方法前mBook: name = 客户端, id-1
客户端调用后 bookin方法后mBook: name = 客户端, id-1

服务端:

 mbook = [Book{name='客户端', id=-1}]

从上面的日志可以发现客户端调用前后book实例是没有任何变化.
服务端也正确读取到客户端发送book实例信息.
回过头来再看看bookIn方法.

  @Overridepublic String bookIn(Book mbook) throws RemoteException {Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");mbook.id = 233;return null;}

方法中bookIn对传入的book对象修改了id为233.但是客户端在调用后并有没有影响自身book对象.

out测试

客户端代码和上面in测试差不多,只改变调用bookout方法而已
客户端:

Book mBook = new Book("客户端", -1);try {Log.e("fmy","客户端没有调用 bookout方法前mBook: name = "+mBook.name+","+" id"+mBook.id);iMyAidlInterface.bookOut(mBook);Log.e("fmy","客户端调用后 bookout方法后mBook: name = "+mBook.name+","+" id"+mBook.id);} catch (RemoteException e) {e.printStackTrace();}

预期结果:
out定义:服务端无法读取从客户端传入的参数,但可以改变传入的对象,然后对客户端的实例对象有影响

客户端没有调用 bookout方法前mBook: name = 客户端, id-1
客户端调用后 bookout方法后mBook: name = null, id233

服务端:

mbook = [Book{name='null', id=0}]

判断正确:因为out是客户端无法传入参数给服务端,所以服务端的book参数都为默认值.
这时服务端修改book的id为233.然后把服务端的book传回.所以调用后客户端的name为空 id为233

AIDL基本使用4—-linkToDeath和unlinkToDeath

这篇关于AIDL基本使用3—-in out inout的用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经