你的位置:首页 > 操作系统

[操作系统]android Service Activity交互之传递复杂数据类型的远程服务


远程服务往往不只是传递java基本数据类型。这时需要注意android的一些限制和规定:

  1. android支持String和CharSequence 
  2. 如果需要在aidl中使用其他aidl接口类型,需要import,即使是在相同包结构下; 
  3. android允许传递实现Parcelable接口的类,需要import; 
  4. android支持集合接口类型List和Map,但是有一些限制,元素必须是基本型或者上述三种情况,不需要import集合接口类,但是需要对元素涉及到的类型import; 
  5. 非基本数据类型,也不是String和CharSequence类型的,需要有方向指示,包括in、out和inout,in表示由客户端设置,out表示由服务端设置,inout是两者均可设置。

举例

 1 package com.mooger.message; 2  3 import android.os.Parcel; 4 import android.os.Parcelable; 5  6 public class SkyMessage implements Parcelable {  7   public String floder; 8   public int aid; 9   public long delay;10   public String head;11   public String body;12   public String foot;13   14   public SkyMessage(String floder ,int aid ,long delay ,String head ,String body ,String foot)15   {16     this.floder = floder;17     this.aid = aid;18     this.delay = delay;19     this.head = head;20     this.body = body;21     this.foot = foot;22   }23   24   @Override 25   public int describeContents() { 26     return 0; 27   } 28   29   @Override 30   public void writeToParcel(Parcel dest, int flags) { 31     dest.writeString(floder);32     dest.writeInt(aid);33     dest.writeLong(delay);34     dest.writeString(head);35     dest.writeString(body);36     dest.writeString(foot);37   } 38 39   public static final Parcelable.Creator<SkyMessage> CREATOR = new Parcelable.Creator<SkyMessage>(){ 40     @Override 41     public SkyMessage createFromParcel(Parcel source) {42       return new SkyMessage(source.readString() ,source.readInt() ,source.readInt() ,source.readString() ,source.readString() ,source.readString()); 43     } 44     45     @Override 46     public SkyMessage[] newArray(int size) { 47       return new SkyMessage[size]; 48     } 49   };50 51 }

然后,需要在相同包下建一个同名的aidl文件,用于android生成相应的辅助文件:

1 package com.mooger.message;2 3 parcelable SkyMessage;

下面是两个aidl文件

SkyActivity.aidl

1 package com.mooger.aidl;2 3 interface SkyActivity{4   void downloadstate(String TAG ,int AID);5 }

SkyService.aidl

 1 package com.mooger.aidl; 2  3 import com.mooger.aidl.SkyActivity; 4 import com.mooger.message.SkyMessage; 5  6 interface SkyService{ 7   void download(String download_url ,String Appname ,int download_size ,int AID ,String floder); 8   void request(int time); 9   void registerSkyCall(SkyActivity at);10   void creatAd(in SkyMessage message);11 }

在自己service类里实现一个内部类继承SkyService并实现相应的方法

 1   public class ServiceBinder extends SkyService.Stub{ 2  3     @Override 4     public void download(String download_url, String Appname, 5         int download_size, int AID ,String floder) throws RemoteException { 6       // TODO Auto-generated method stub 7       ..... 8     } 9 10     @Override11     public void registerSkyCall(SkyActivity at) throws RemoteException {12       // TODO Auto-generated method stub13       ......14     }15 16     @Override17     public void request(int time) throws RemoteException {18       // TODO Auto-generated method stub19       .......20     }21 22     @Override23     public void creatAd(SkyMessage message) throws RemoteException {24       // TODO Auto-generated method stub25       .........26     }  27   }

创建 ServiceBinder 实例

1 private ServiceBinder serviceBinder = new ServiceBinder();

并在onBind方法中返回实例

1 @Override2 public IBinder onBind(Intent intent) {3    Log.d(TAG, "Service onBind");4     return serviceBinder;5   }

在activity中实例化一个ServiceConnection对象并实现相应方法

 1   private SkyService downloadService; 2    3   private ServiceConnection serviceConnection = new ServiceConnection(){ 4  5     @Override 6     public void onServiceConnected(ComponentName name, IBinder service) { 7       // TODO Auto-generated method stub 8       Log.d("BootStart","activity onServiceConnected"); 9       downloadService = SkyService.Stub.asInterface(service);10       try {11         downloadService.registerSkyCall(AtCallback);12       } catch (RemoteException e) {13         // TODO Auto-generated catch block14         e.printStackTrace();15       }16     }17 18     @Override19     public void onServiceDisconnected(ComponentName name) {20       // TODO Auto-generated method stub21       downloadService = null;22     }23     24   };

最后通过bindService方法实现绑定

1 this.bindService(new Intent("com.mooger.aidl.Service"), this.serviceConnection, BIND_AUTO_CREATE);//绑定到服务

AndroidManifest.中的service设置

1 <service android:name="com.mooger.service_133.MooService"     android:label="@string/service_name"2 android:icon="@drawable/ic_launcher">3    <intent-filter>4     <action android:name="com.mooger.aidl.Service" /> 5   </intent-filter>      6 </service>

这样就完成了基本的设置和部署,通信传递数据只要向下面的例子代码即可

1 SkyMessage message = new SkyMessage(folder, jsonObj.optInt("aid"), jsonObj.optLong("delay"), jsonObj.optString("head"), jsonObj.optString("body"), jsonObj.optString("foot"));2 try {  3     downloadService.creatAd(message);                 4 } catch (RemoteException e) {5   // TODO Auto-generated catch block6   e.printStackTrace();7 }