How to determine a valid interface type in a .aidl file?

I have a .aidl file that defines a single possible type of interface, say

parcelable MyInterface; 

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 a specific implementation and therefore cannot implement the createFromParcel () method?

How will the android runtime work, from which CREATOR (from which subclass) to call? Is it not possible to use an interface type in a .aidl file?

+6
source share
3 answers
  • about using the interface in an AIDL file: I don’t think there is anything stopping you from doing this. Because "sending MyInterface"; it doesn’t actually generate anything in the gen folder, it is just necessary for the function signature of any AIDL interface using this type of MyInterface.

  • CREATOR You must add a developer definition for all your classes implementing android.os.Par.com.

+3
source

Deploy Parcelable via AIDL

First step: - Create another .aidl file that will be used to define the Student class (Parcelable class).

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

we write this because helpl can detect the student class.

Second step: now you need to define a java class called student and implement the parcable interface in this class. parcable has two abstract methods that you must implement in your 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 the transfer 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 that implements Parcelable, you must provide the CREATOR field. The type of CREATOR must be Parcelable.Creator. Here, instead of T, we write the name of our class, for example. Student. CREATOR is used during the UnMarshalling 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 information: check here

+3
source

I came across a similar scenario and wanted to share what I did. I had the following main interface, which contains another interface.

 //ICounterAidlInterface.aidl import path.to.aidldirectory.CounterListener interface ICounterAidlInterface { int getCounter(); void setCounterListener(CounterListener listener); } 

Do not forget to import.

The question is how to introduce this new type: CounterListener . Since CounterListener itself is an interface, you do not need to specify it whenever possible.

You need to create another help file for CounterListener . So, I created another helpl file:

 //CounterListener.aidl interface CounterListener{ void newData(int data); } 

Hope this helps :)

0
source

Source: https://habr.com/ru/post/899443/


All Articles