Android Service Activity: remote Service that transmits complex data types, androidactivity
Remote services often do not just pass the Basic java data type. In this case, pay attention to some android restrictions and regulations:
Example
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 }
Then, you need to create an aidl file with the same name under the same package for android to generate the corresponding auxiliary file:
1 package com.mooger.message;2 3 parcelable SkyMessage;
The following are two aidl files.
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 }
Implement an internal class in your own service class to inherit SkyService and implement corresponding methods
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 }
Create a ServiceBinder instance
1 private ServiceBinder serviceBinder = new ServiceBinder();
And returns the instance in the onBind method.
1 @Override2 public IBinder onBind(Intent intent) {3 Log.d(TAG, "Service onBind");4 return serviceBinder;5 }
Instantiate a ServiceConnection object in activity and implement corresponding methods
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 };
Finally, bindService is used for binding.
1 this. bindService (new Intent ("com. mooger. aidl. Service"), this. serviceConnection, BIND_AUTO_CREATE); // bind to the Service
AndroidManifest. xmlService settings in
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>
In this way, the basic settings and deployment are completed, and only the following sample code can be used to transmit data through communication.
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 }