标签:
远程服务往往不只是传递java基本数据类型。这时需要注意android的一些限制和规定:
举例
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 @Override 11 public void registerSkyCall(SkyActivity at) throws RemoteException { 12 // TODO Auto-generated method stub 13 ...... 14 } 15 16 @Override 17 public void request(int time) throws RemoteException { 18 // TODO Auto-generated method stub 19 ....... 20 } 21 22 @Override 23 public void creatAd(SkyMessage message) throws RemoteException { 24 // TODO Auto-generated method stub 25 ......... 26 } 27 }
创建 ServiceBinder 实例
1 private ServiceBinder serviceBinder = new ServiceBinder();
并在onBind方法中返回实例
1 @Override 2 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 block 14 e.printStackTrace(); 15 } 16 } 17 18 @Override 19 public void onServiceDisconnected(ComponentName name) { 20 // TODO Auto-generated method stub 21 downloadService = null; 22 } 23 24 };
最后通过bindService方法实现绑定
1 this.bindService(new Intent("com.mooger.aidl.Service"), this.serviceConnection, BIND_AUTO_CREATE);//绑定到服务
AndroidManifest.xml 中的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 block 6 e.printStackTrace(); 7 }
android Service Activity交互之传递复杂数据类型的远程服务
标签:
原文地址:http://www.cnblogs.com/AndyZhengLL/p/4779026.html