Binder Mechanism, from Java to C (4. Parcel)

Source: Internet
Author: User

1. The data of remote calls is transmitted in the remote function. Some data must be transmitted. How can this data be transmitted? In IPC, the Proxy end is used to package some parameters and then send them out. The following is the code that is frequently encountered on the Proxy end to call remote methods. We can see that the sent and received data is packaged by Parcel. Copy code 1 class ActivityManagerProxy implements IActivityManager 2 {3 public int startActivity (IApplicationThread caller, Intent intent, 4 String activities, IBinder resul.pdf, String resultWho, int requestCode, 5 int startFlags, String profileFile, 6 ParcelFileDescriptor profileFd, Bundle options) throws RemoteException {7 Parcel data = Parcel. obtain (); 8 Parcel reply = Parcel. obtain (); 9 data. wr IteInterfaceToken (IActivityManager. descriptor); 10 data. writeStrongBinder (caller! = Null? Caller. asBinder (): null); 11 intent. writeToParcel (data, 0); 12 data. writeString (resolvedType); 13 data. writeStrongBinder (resulinder); 14 data. writeString (resultWho); 15... 16 mRemote. transact (START_ACTIVITY_TRANSACTION, data, reply, 0); 17 reply. readException (); 18 int result = reply. readInt (); 19... 20 return result; 21} 22} copy some basic types of code, such as String and int, which can be directly written into Parcel. For some complex data types, such as some custom objects, how can I write it into Parcel? When we use a new object class, the valid range of this object is only in a process. It is unrealistic to pass an object directly through the IPC Mechanism. Because objects in the Java environment are accessed through references, an object accesses other objects through a reference similar to a pointer, and accesses with similar addresses are involved. Therefore, when passing a complex object, the object must be split into an intermediate format, and then transmitted in IPC. So how can we split an object into an intermediate form? --> This operation requires the Parcelable interface. 2. Parcelable when an object needs to be transmitted between processes, this object must implement the Parcelable interface attributes or methods. For example, in a single process, there is a class: Copy code 1 class TaskInfo {2 public int mPid; 3 public int mUid; 4 public String mPackageName; 5 6 TaskInfo () {7 mPid =-1; 8 mUid =-1; 9 mPackageName = null; 10} after the code is Parcelable, it can be transferred across processes. The defined TaskInfo class: copy code 1 public class TaskInfo implements Parcelable {2 public int mPid; 3 public int mUid; 4 public String mPackageName; 5 6 TaskInfo () {7 mPid =-1; 8 mUid =-1; 9 mPackgeNa Me = null; 10} 11 public int describeContents () {// the return value describes what role Parcel plays, which is generally 0.12 return 0; 13} 14 15 public void writeToParcel (Parcel out, int flags) {// used for sending, write the attributes to be transmitted to Parcel. 16 out. writeInt (mPid); 17 out. writeInf (mUid); 18 out. writeString (mPackageName); 19} 20 21 public static final Parcelable. creator <TaskInfo> CREATOR = new Parcelable. creator <TaskInfo> () {// corresponds to writeToParcel (). The process at the receiving end returns the intermediate object of the Parcel object and passes the new object. 22 public TaskInfo createFromParcel (Parcel in) {// initialize Parcel object23 return new TaskInfo (in); 24} 25 26 public TaskInfo [] newArray (int size) {// creates multiple empty objects and uses the default initialization method. 27 return new TaskInfo [size]; 28} 29}; 30 private TaskInfo (Parcel in) {// use Parcel's readInt () and readLong () 31 mPid = in. readInt (); 32 mUid = in. readInt (); 33 mPackageName = in. readString (); 34} 35} copy the code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.