Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define parcelable of interface type in .aidl file?

I have an .aidl file that defines a single parcelable of an interface type, let's say

parcelable MyInterface;

Whereby MyInterface is a java interface declared in MyInterface.java that extends the Parcelable interface. The android parcelable mechanism requires me to define a static CREATOR in the parcelable class. But how can I do this for an interface since the interface class does not know the concrete implementation and therefor cannot implement the createFromParcel() method?

How will the android runtime decide which CREATOR (from which subclass) to call? Is it even impossible to use an interface type in an .aidl file?

like image 409
Landschaft Avatar asked Oct 17 '11 09:10

Landschaft


People also ask

What is the Android interface definition language?

The Android Interface Definition Language (AIDL) is a tool that lets users abstract away IPC. Given an interface (specified in a . aidl file), various build systems use the aidl binary to construct C++ or Java bindings so that this interface can be used across processes, regardless of the runtime or bitness there.

What is difference between HIDL and AIDL?

AIDL has been around longer than HIDL, and is used in many other places, such as between Android framework components or in apps. Now that AIDL has stability support, it's possible to implement an entire stack with a single IPC runtime. AIDL also has a better versioning system than HIDL.


2 Answers

Implement the Parcelable over AIDL

First Step:- Create a different .aidl file that will be used for define the Student class (Parcelable class).

       (Student.aidl)
         package com.aidl; 
         parcelable Student;

we write this because aidl can detect Student class.

Second Step:- now you have to define a java class with name student and implement parcable interface in this class. parcable interface has two abstract method that you have to implement in your student class.

    import android.os.Parcel;
    import android.os.Parcelable;
    public class Student implements Parcelable {
            public String name;
            public String father_name;
            public Student(Parcel source)
            {
                            name = source.readString();
                            father_name = source.readString();
            }
            public Student()
            {}
            public void setName(String name)
            {
                            this.name = name;
            }
            public void setFatherName(String father_name)
            {
                            this.father_name = father_name;
            }

// methods of parcable interface

            @Override
            public int describeContents() {
                            // TODO Auto-generated method stub
                            return 0;
            }
            @Override
            public void writeToParcel(Parcel dest, int flags) {
                            // TODO Auto-generated method stub
                            dest.writeString(name);
                            dest.writeString(father_name);
                           
            }

In any class which is implementing Parcelable have to provide CREATOR field. The type of CREATOR must be Parcelable.Creator. Here in place of T we write the name of our class eg. Student. CREATOR is used during UnMarshalling of the object.

It has two methods –

1-T createFromParcel(Parcel parcel) :This method is called when UnMarshalling happen 
  during receiving the data. Keep care that  we receive the data member in same sequence
  as we write in writeToPacel(). Here we create a  constructor in which we demarshalling
  the data.
2-NewArray(int size) : Here we just create an array of given size and return.

        
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
               @Override
               public Student createFromParcel(Parcel source) {
                                            // TODO Auto-generated method stub
                                            return new Student(source);
                }
                @Override
                public Student[] newArray(int size) {
                                            // TODO Auto-generated method stub
                                            return new Student[size];
                 }
            };

for more info: Check Here

like image 131
Pankaj Arora Avatar answered Sep 18 '22 10:09

Pankaj Arora


  1. about use interface in AIDL file: I don't think there is anything there stopping you to do so. Because "parcelable MyInterface;" does not actually generate anything in gen folder, it is just needed for function signature of any AIDL interface using this MyInterface type.

  2. CREATOR You have to add creator definition for all your classes implements android.os.Parcelable.

like image 27
zhao chang Avatar answered Sep 21 '22 10:09

zhao chang