一、AIDL Service简介
Android系统中,各个应用都运行在自己的进程中,进程之间一般无法直接进行通信,为了实现进程通信,Android提供了AIDL Service;
二、与本地Service不同
本地Service:直接把IBinder对象本身传递给客户端的ServiceConnection的onServiceConnected方法的第二个参数;1. 创建AIDL文件,定义好的AIDL文件后,ADT工具会自动在gen目录下生成一个AIDL.java接口,该类内部包含一个Stub内部类,实现了IBinder,AIDL里面的接口,这个Stub类会作为远程Service回调类:
IMyService.aidl
package com.juno.serviceaidltest;
import com.juno.serviceaidltest.Product;
interface IMyService
{
String getValue();
Map getMap(in String country, in Product product);
Product getProduct();
}
Product.aidl
parcelable Product;
Product.java
package com.juno.serviceaidltest;
import android.os.Parcel;
import android.os.Parcelable;
public class Product implements Parcelable {
private int id;
private String name;
private float price;
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Product createFromParcel(Parcel in) {
return new Product(in);
}
public Product[] newArray(int size) {
return new Product[size];
}
};
public Product() {
}
private Product(Parcel in) {
readFromParcel(in);
}
@Override
public int describeContents() {
return 0;
}
public void readFromParcel(Parcel in) {
id = in.readInt();
name = in.readString();
price = in.readFloat();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeFloat(price);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
MyService.java
package com.juno.serviceaidltest;
import java.util.HashMap;
import java.util.Map;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
/**
* 继承Stub,也就是实现了IMyService接口,并实现了IBinder接口
*/
public class MyServiceImpl extends IMyService.Stub {
@Override
public String getValue() throws RemoteException {
return "Test Value";
}
@Override
public Map getMap(String country, Product product)
throws RemoteException {
Map map = new HashMap();
map.put("country", country);
map.put("id", product.getId());
map.put("name", product.getName());
map.put("price", product.getPrice());
map.put("product", product);
return map;
}
@Override
public Product getProduct() throws RemoteException {
Product product = new Product();
product.setId(1234);
product.setName("汽车");
product.setPrice(31000);
return product;
}
}
@Override
public IBinder onBind(Intent intent) {
/**
* 返回MyServiceImpl对象,在绑定本地Service情况下,该MyServiceImpl会直接传给客户端的ServiceConnected对象的ServiceConnected()方法的第二个参数;在绑定远程Service的情况下,只将MyServiceImpl对象的代理传给客户端的ServiceConnected对象的ServiceConnected()方法的第二个参数
*/
return new MyServiceImpl();
}
}
MainActivity.java
package com.juno.serviceanotheraidltest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.juno.serviceaidltest.IMyService;
public class MainActivity extends Activity implements View.OnClickListener {
private final static String ACTION = "com.juno.serviceaidltest.IService";
private IMyService myService = null;
private Button mBtnInvokeAIDLService;
private Button mBtnBindAIDLService;
private TextView mTextView;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//获取远程Service的onBinder方法返回的对象代理
myService = IMyService.Stub.asInterface(service);
mBtnInvokeAIDLService.setEnabled(true);
try {
Log.v("juno", myService.getValue());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBtnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);
mBtnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService);
mBtnInvokeAIDLService.setEnabled(false);
mTextView = (TextView) findViewById(R.id.textView1);
mBtnInvokeAIDLService.setOnClickListener(this);
mBtnBindAIDLService.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBindAIDLService:
//创建所需要绑定的Service的Intent,绑定远程的服务
bindService(new Intent(ACTION), mServiceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.btnInvokeAIDLService:
try {
String s = myService.getValue();
s = "Product.id = " + myService.getProduct().getId() + "\n";
s += "Product.name = " + myService.getProduct().getName()
+ "\n";
s += "Product.price = " + myService.getProduct().getPrice()
+ "\n";
s += myService.getMap("China", myService.getProduct()).toString();
mTextView.setText(myService.asBinder().isBinderAlive() + " " + s);
} catch (Exception e) {
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (myService != null) {
//解除绑定
unbindService(mServiceConnection);
}
}
}
activity_main.xml