2011-10-17 13 views
6

Mam plik .aidl który definiuje pojedynczy parcelable danego typu interfejsu, powiedzmyJak zdefiniować działka z typem interfejsu w pliku .aidl?

parcelable MyInterface; 

Przy czym MyInterface jest interfejs Java zadeklarowane w MyInterface.java że rozszerza Parcelable interfejs. Mechanizm Android działka wymaga, aby zdefiniować statyczny CREATOR w klasie działek. Ale jak mogę to zrobić dla interfejsu, ponieważ klasa interfejsu nie zna konkretnej implementacji i dlatego nie może zaimplementować metody createFromParcel()?

W jaki sposób środowisko wykonawcze do systemu Android zdecyduje, do którego producenta (z której subklasy) należy zadzwonić? Czy jest nawet niemożliwe użycie typu interfejsu w pliku .aidl?

+0

Czy go rozwiązać? –

Odpowiedz

3
  1. o interfejs użytkowania w pliku AIDL: Nie sądzę, nie ma nic nie powstrzymuje to zrobić. Ponieważ "działka MyInterface;" w rzeczywistości nie generuje niczego w folderze gen, jest to potrzebne do podpisu funkcji dowolnego interfejsu AIDL za pomocą tego typu MyInterface.

  2. CREATOR Musisz dodać definicję kreatora dla wszystkich swoich klas narzędzi android.os.Parcelable.

3

Wdrożenie Parcelable nad AIDL

Pierwszy krok: - Utwórz inny plik .aidl, który będzie używany do zdefiniowania klasy Student (Parcelable klasy).

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

piszemy to, ponieważ helpl może wykryć klasę studentów.

Krok drugi: - musisz teraz zdefiniować klasę java ze studentem name i implementować interfejs parcable w tej klasie. Interfejs parcable ma dwie abstrakcyjne metody, które musisz zastosować w klasie studenta.

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; 
      } 

// metody parcable interfejsu

  @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); 

      } 

z każdej klasy implementujący Parcelable trzeba zapewnić pole twórcy. Typem twórcy musi być Parcelable.Creator. Tutaj zamiast T wpisujemy nazwę naszej klasy np. Student. TWÓRCA jest używany podczas UnMarshallingu obiektu.

Posiada dwie metody -

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]; 
       } 
      }; 

o więcej informacji: Check Here

0

wpadłem na podobny scenariusz, i chciałem podzielić się co zrobiłem. Miałem następujący interfejs główny pomocy, który zawiera inny interfejs wewnątrz niego.

//ICounterAidlInterface.aidl 

import path.to.aidldirectory.CounterListener 

interface ICounterAidlInterface { 
    int getCounter(); 
    void setCounterListener(CounterListener listener); 
} 

Nie zapomnij o imporcie.

Pytanie brzmi, jak reprezentować ten nowy typ: CounterListener.Ponieważ interfejs CounterListener jest interfejsem, nie trzeba go oznaczać jako działki.

Należy również utworzyć inny plik pomocy dla CounterListener. Więc stworzyłem kolejny plik aidl:

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

Hope this helps :)

Powiązane problemy