Using self-defined Parcelable objects during an Android AIDL RPC / IPC call
Using self-defined Parcelable objects during an Android AIDL RPC / IPC call
In my previous post “Using the Android Interface Definition Language (AIDL) to make a Remote Procedure Call (RPC) in Android” I’ve explained the basics on how inter-process communication can be implemented in Android. Now we will take a look at a specialized field in this area: Using Parcelables as parameters of an AIDL method.
As described in the previous post it is possible to either use primitive
java types within a remote method signature or any class which
implements the android.os.Parcelable interface. Using this interface it
is possible to define arbitrarily complex data types which can be used
as parameters or return values of methods during a remote method call.
I’ve created a modified version of the first example app to give you a
basic example which relies on a Parcelable object as a method return
value.
The example consists of a Service which generates a Parcelable message
object containing the current system time and text appearance values
such as the color, the size and the style. In the second part of the
example you can find an Activity which is using the AIDL IPC to retrieve
such a message object to display the current time. The corresponding
projects are called AIDLRemoteClientUsingParcelableObject for the client
project (containing the Activity) and
AIDLRemoteMessageServiceUsingParcelableObject for the server (containing
the Service).
Because this example is just a modified version of the previous one I
will only explain the Parcelable related parts in this post. If you
encounter trouble understanding the context, please refer to the first posting on AIDL.
As always you can browse or download the source code of the example App at our SVN repository at Google Code (http://code.google.com/p/android-aidl-ipc-rpc-example/) or use svn to check the project out and your allowed to reuse portions of this code as you wish.
Fundamentals of Parcels and Parcelables
In Android, Parcels are used to transmit messages. Unlike to the java
serialization, the Parcels are implanted as high-performance containers
for the Android inter process communication. By implementing the
Parcelable interface you declare that it’s possible to transform
(marshall) your class into a Parcel and back (demarshall). Because the
Parcels are designed for performance you should always use Parcelables
instead of using the java serialization (which would also be possible)
when doing IPC in android. Even when you are communicating with Intents
you can still use Parcels to pass data within the intent instead of
serialized data which is most likely not as efficient as Parcels.
The Parcelable Interface
The android.os.Parcelable interface defines two methods which have to be implemented:
int describeContents()
This method can be used to give additional hints on how to process the received parcel. For example, there could be multiple implementations of an Interface which extends the Parcelable Interface. When such a parcel is received, this method can then be to determine which object implementation needs to be instantiated.
void writeToParcel(Parcel dest, int flags)
his is the core method which is called when this object is to be marshalled to a parcel object. In this method all required data fields should be added to the “dest” Parcel so that it’s possible to restore the state of the object within the receiver during the demarshalling. The “flags” parameter indicates if the marshalling was triggered because the object becomes a return value of a remote method (if that’s the case the parameter is set to Parcelable.PARCELABLE_WRITE_RETURN_VALUE). If your model object contains references which could hinder the garbage collector from freeing the resources, you may want to check if the flag is set and remove the references at this moment to free resources which are limited on mobile devices.
Furthermore it is necessary to provide a static CREATOR field in any implementation of the Parcelable interface. The type of this CREATOR must be of Parcelable.Creator<T>. This CREATOR will act as a factory to create objects during the demarshalling of the parcel. For this purpose, the Parcelable.Creator interface defines two methods where the type parameter T represents the Parcelable object:
T createFromParcel(Parcel source)
This method is called during the demarshalling of the object. The parameter source represents the parcel which contains the data of the corresponding object. During this method you can extract the data fields in the same sequence as you put them into the Parcel during the writeToParcel method. These fields can then be used to create an instance of the object.
T[] newArray(int size)
This method returns just an empty array of the object with a given size.
Defining the Parcelable Message Class
Now that we know what we have to implement when creating a class which implements the Parcelable interface, we can define our own message class which will be used in the example. Within the example source code, the MyParcelableMessage class is defined in the com.appsolut.example.aidlMessageServiceUsingParcelable package.
public class MyParcelableMessage implements Parcelable {
private final String message;
private final int textSize;
private final int textColor;
private final Typeface textTypeface;
These fields represent the state of the message. We have to make sure that the information stored there is not lost during the marshalling or demarshalling process. For the marshaling process, we need to implement the methods defined in the Parcelable interface:
The describeContents Method
public int describeContents() {
return 0;
}
Because our content has nothing special about it we can just return a zero.
The writeToParcel Method
The writeToParcel method is implemented quite easy as well. The Parcel interface contains methods to write primitve java types such as string, int, etc. We can use these methods to store the object state within the parcel.
public void writeToParcel(Parcel outParcel, int flags) {
outParcel.writeString(message);
outParcel.writeInt(textSize);
outParcel.writeInt(textColor);
outParcel.writeInt(textTypeface.getStyle());
}
For the demarshalling, we need to remember the sequence in which we have stored the fields in the parcel.
The Parcelable.Creator CREATOR field
The CREATOR field is required for the demarshalling. As previously described, the Parcelable.Creator interface requires us to implement two methods:
public static final Parcelable.Creator<MyParcelableMessage> CREATOR = new Parcelable.Creator<MyParcelableMessage>() {
@Override
public MyParcelableMessage createFromParcel(Parcel in) {
String message = in.readString();
int fontSize = in.readInt();
int textColor = in.readInt();
Typeface typeface = Typeface.defaultFromStyle(in.readInt());
return new MyParcelableMessage(message, fontSize, textColor, typeface);
}
@Override
public MyParcelableMessage[] newArray(int size) {
return new MyParcelableMessage[size];
}
};
During the createFromParcel method we use the read methods which are provided in the Parcel to extract our state information. In the end we create a new MyParcelableMessage object with the corresponding state.
The newArray method just returns an empty array.
Defining the AIDL Files
The AIDL complier won’t be able to locate our self-defined MyParcelableMessage even if it implements the Parcelable interface. To propagate our implementation to the AIDL compiler, we need to define an aidl file which declares the class as Parcelable:
/* The package where the aidl file is located */
package com.appsolut.example.aidlMessageServiceUsingParcelable; /* Declare our message as a class which implements the Parcelable interface */
parcelable MyParcelableMessage;
Like all aidl files, this file has to be in the same folder as the class file.
Within the aidl file of the remote interface we have to import our Parcelable message so that the AIDL compiler knows about it. This is done by an java like import statement where the full identifier of the class has to be used (package + class).
/* The package where the aidl file is located */
package com.appsolut.example.aidlMessageServiceUsingParcelable; /* Import our Parcelable message */
import com.appsolut.example.aidlMessageServiceUsingParcelable.MyParcelableMessage; /* The name of the remote service */
interface IRemoteParcelableMessageService { /* A simple Method which will return a message
* The message object implements the Parcelable interface
*/
MyParcelableMessage getMessage(); }
Using the Parcelable Class in the Client
In addition to the AIDL remote interface description, you have to provide the defined Parcelable classes to the client because the class files are required to instantiate the objects when performing the remote procedure call. In our example we just copied the .java file together with the corresponding .aidl files to the client project.
Summary and Further Reading
It is quite easy to define own model classes which are Parcelable. So you should always prefer parcels over the use of the default java serialization because the performance can be increased, which is important on mobile devices which might not always use the latest processor or be limited by other resources e.g. the memory. In addition to the performance enhancements, the possibility of using Parcelable model classes further enhances the capabilities of the Android IPC mechanism and increases the ease of use of the remote interface because multiple parameters can be combined into an object.
If you want to get more information on this topic I suggest you refer to the following links:
- Android Interface Definition Language (AIDL)
- Android Interface Definition Language (AIDL): Passing Objects
- Android API Reference: Parcel
- Android API Reference: Parcelable
- Android API Reference: Parcelable.Creator
- Android API Reference: IBinder
http://www.app-solut.com/blog/2011/05/using-self-defined-parcelable-objects-during-an-android-aidl-rpc-ipc-call/
by Kevin Kratzer
Using self-defined Parcelable objects during an Android AIDL RPC / IPC call的更多相关文章
- android AIDL RPC 机制
AIDL 这是接口文件的叙述性说明,为了实现android 上述平台RPC ,aapt 在编译时自己主动按照该规则IPC 的接口和对象,作为一个用户只需要 实现在服务侧的界面 2 在clientbin ...
- Android中的IPC机制
Android IPC简介 IPC是Inter-Process Communication的缩写,含义就是进程间通信或者跨进程通信,是指两个进程之间进行数据交换的过程.那么什么是进程,什么是线程,进程 ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- Android开发之IPC进程间通信-AIDL介绍及实例解析
一.IPC进程间通信 IPC是进程间通信方法的统称,Linux IPC包括以下方法,Android的进程间通信主要采用是哪些方法呢? 1. 管道(Pipe)及有名管道(named pipe):管道可用 ...
- Android AIDL 实例
为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface Def ...
- android AIDL示例代码(mark下)
1.demo结构图 2.ipcclient Book类. package com.mu.guoxw.ipcclient; import android.os.Parcel; import androi ...
- (转载)你真的理解Android AIDL中的in,out,inout么?
前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了——或者并没有,但所占篇幅甚小,且基本上都是 ...
- Android AIDL使用详解_Android IPC 机制详解
一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...
- Android下的IPC通信方式
一.Bundle Android的Activity.Service.Receiver都支持在Intent传递Bundle数据,Bundle实现了Parcelable接口, 所以能很方便的在不同进程之间 ...
随机推荐
- xcode6 真机运行报错 Command /usr/bin/codesign failed with exit code 1
解决方法: 百度下载‘iphone配置实用工具’, 打开此软件之后,选择预配置描述文件, 选中iphone过期和重复的描述文件,按delete键删除.然后重启xcode即可
- javascript函数的声明和调用
× 目录 [1]函数的声明方式 [2]函数的调用方式 [3]两种声明方式的区别 函数:将完成某一特定功能的代码集合起来,可以重复使用的代码块. ---------------------------- ...
- android四种更新UI的方法
笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...
- LinearLayout和RelativeLayout
LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件android:id="@+id/btn1" 控件宽度android:layo ...
- Linux学习笔记---【1】
什么是POSIX? 为何说Linux使用POSIX对于开发有很好的影响? POSIX是可携式操作系统接口(Portable Operating System Interface)的缩写,重点在于规范内 ...
- ArrayList与List对象用法与区别
比如: 代码如下 复制代码 string[] s=new string[3];//赋值s[0]="a";s[1]="b";s[2]="c"; ...
- 【原创】Oracle函数中对于NO_DATA_FOUND异常处理的研究
一直以来有一个困惑,一直没解决,昨天一哥们问我这个问题,决心弄清楚,终于得到了答案.先看下面这个函数: create or replace function fn_test(c_xm varchar) ...
- dataAdapter与dataSet和dataTable的填充
对于dataAdapter与dataSet和dataTable的填充,可以分为1对1,1对n,n对n,3种情况. 以SqlDataAdapter为例. //(1)1对1 SqlDataAdapter ...
- 关于css的一些小细节---link
<link rel="stylesheet" href=“a.css” type="text/css"> rel:当前文档与被链接文档间的关系,必须 ...
- 10款强大的jQuery/HTML5应用新鲜出炉
1.CSS3/jQuery自定义弹出窗口 多种弹出动画 这是一款利用jQuery和CSS3实现的自定义弹出窗口,这可比浏览器默认的弹出窗口漂亮多了.弹出窗口中可以自定义html,十分灵活.另外最重要的 ...