2011-10-17 29 views
6

私はインターフェイスタイプの単一parcelableを定義.aidlファイルを持っている、MyInterfaceはParcelableインタフェースを拡張MyInterface.javaで宣言されたJavaのインタフェースとなるのは、.aidlファイルでインターフェイスタイプのパーセルブルを定義する方法は?

parcelable MyInterface; 

をしましょう。アンドロイドのパーセル可能なメカニズムでは、パーセル可能なクラスに静的なCREATORを定義する必要があります。しかし、インターフェイスクラスが具体的な実装を知らないため、createFromParcel()メソッドを実装できないため、これをどのようにしてインターフェイスに対して行うことができますか?

アンドロイドランタイムはどのCREATOR(どのサブクラスから)を呼び出すのですか? .aidlファイルにインターフェイスタイプを使用することも不可能ですか? AIDLファイルで使用するインタフェースに関する

+0

をあなたはそれを解決しましたか? –

答えて

3
  1. : 私はそうするように、あなたを停止することが何もないと思います。 "parcelable MyInterface;"実際にはgenフォルダに何も生成されませんが、このMyInterface型を使用するAIDLインタフェースの関数シグネチャに必要です。

  2. CREATOR すべてのクラスのクリエイター定義を追加する必要があります。アンドロイド.os.Parcelableを実装しています。

3

Parcelable

AIDL以上の第一歩実装: - Studentクラス(Parcelableクラス)を定義するために使用される異なる.aidlファイルを作成しますを。

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

aidlがStudentクラスを検出できるので、これを書きます。

第2ステップ: -ここで、studentという名前のjavaクラスを定義し、このクラスにparcableインターフェイスを実装する必要があります。 parcableインターフェースには、学生クラスに実装する必要がある2つの抽象メソッドがあります。 Parcelableを実装している任意のクラスで

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

// parcableインターフェース方法

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

      } 

CREATOR場を提供しなければなりません。 CREATORのタイプはParcelable.Creatorでなければなりません。ここでTの代わりに私たちはクラスの名前を書きます。学生。 CREATORは、オブジェクトのアンマーシャリング中に使用されます。 Check Here

0

私は同様のシナリオに走った、と私が何をしたか共有したいと思った:詳細は

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

-

それは二つの方法があります。私はそれの内部に別のインターフェイスを含む補助インターフェイスメインインターフェイスに従っていた。

//ICounterAidlInterface.aidl 

import path.to.aidldirectory.CounterListener 

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

インポートを忘れないでください。

この新しいタイプの表現方法は、CounterListenerです。CounterListener自体がインターフェイスなので、それを分割可能とする必要はありません。

CounterListenerの別の補助ファイルを作成する必要があります。だから、私は別のAIDLファイル作成:

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

希望をこのことができます:)

関連する問題