android Service Activity交互之传递复杂数据类型的远程服务

时间:2024-10-22 09:33:08

远程服务往往不只是传递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是两者均可设置。

举例

 package com.mooger.message;

 import android.os.Parcel;
import android.os.Parcelable; public class SkyMessage implements Parcelable {
public String floder;
public int aid;
public long delay;
public String head;
public String body;
public String foot; public SkyMessage(String floder ,int aid ,long delay ,String head ,String body ,String foot)
{
this.floder = floder;
this.aid = aid;
this.delay = delay;
this.head = head;
this.body = body;
this.foot = foot;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(floder);
dest.writeInt(aid);
dest.writeLong(delay);
dest.writeString(head);
dest.writeString(body);
dest.writeString(foot);
} public static final Parcelable.Creator<SkyMessage> CREATOR = new Parcelable.Creator<SkyMessage>(){
@Override
public SkyMessage createFromParcel(Parcel source) {
return new SkyMessage(source.readString() ,source.readInt() ,source.readInt() ,source.readString() ,source.readString() ,source.readString());
} @Override
public SkyMessage[] newArray(int size) {
return new SkyMessage[size];
}
}; }

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

 package com.mooger.message;

 parcelable SkyMessage;

下面是两个aidl文件

SkyActivity.aidl

 package com.mooger.aidl;

 interface SkyActivity{
void downloadstate(String TAG ,int AID);
}

SkyService.aidl

 package com.mooger.aidl;

 import com.mooger.aidl.SkyActivity;
import com.mooger.message.SkyMessage; interface SkyService{
void download(String download_url ,String Appname ,int download_size ,int AID ,String floder);
void request(int time);
void registerSkyCall(SkyActivity at);
void creatAd(in SkyMessage message);
}

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

     public class ServiceBinder extends SkyService.Stub{

         @Override
public void download(String download_url, String Appname,
int download_size, int AID ,String floder) throws RemoteException {
// TODO Auto-generated method stub
.....
} @Override
public void registerSkyCall(SkyActivity at) throws RemoteException {
// TODO Auto-generated method stub
......
} @Override
public void request(int time) throws RemoteException {
// TODO Auto-generated method stub
.......
} @Override
public void creatAd(SkyMessage message) throws RemoteException {
// TODO Auto-generated method stub
.........
}
}

创建 ServiceBinder 实例

 private ServiceBinder serviceBinder = new ServiceBinder();

并在onBind方法中返回实例

 @Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Service onBind");
return serviceBinder;
}

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

     private SkyService downloadService;

     private ServiceConnection serviceConnection = new ServiceConnection(){

         @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.d("BootStart","activity onServiceConnected");
downloadService = SkyService.Stub.asInterface(service);
try {
downloadService.registerSkyCall(AtCallback);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
downloadService = null;
} };

最后通过bindService方法实现绑定

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

AndroidManifest.xml 中的service设置

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

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

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