AIDL的作用
AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。
AIDL IPC机制是面向接口的,像COM或Corba一样,但是更加轻量级。它是使用代理类在客户端和实现端传递数据。
实现的简单列子
下面小编写了一个简单登录
1.MainActivity.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyQQ登录"
android:textSize="30sp"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入MyQQ号:"
android:inputType="number"
android:id="@+id/et_main_num"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入MyQQ密码:"
android:inputType="numberPassword"
android:id="@+id/et_main_pass"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="login"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结果:"
android:id="@+id/tv_main_result"
android:textSize="25sp"
/>
2.QQUser.java
public class QQUser implements Parcelable{
private String num;
private String pass;
private String netName;
private String address;
private String sex;
private String age;
public QQUser() {
}
public QQUser(String num, String pass, String netName, String address, String sex, String age) {
this.num = num;
this.pass = pass;
this.netName = netName;
this.address = address;
this.sex = sex;
this.age = age;
}
protected QQUser(Parcel in) {
num = in.readString();
pass = in.readString();
netName = in.readString();
address = in.readString();
sex = in.readString();
age = in.readString();
}
public static final Creator<QQUser> CREATOR = new Creator<QQUser>() {
@Override
public QQUser createFromParcel(Parcel in) {
return new QQUser(in);
}
@Override
public QQUser[] newArray(int size) {
return new QQUser[size];
}
};
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getNetName() {
return netName;
}
public void setNetName(String netName) {
this.netName = netName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(num);
parcel.writeString(pass);
parcel.writeString(netName);
parcel.writeString(address);
parcel.writeString(sex);
parcel.writeString(age);
}
}
3.MyQQLoginService.java
public class MyQQLoginService extends Service {
class MyIBinder extends QQLoginOut.Stub{
@Override
public boolean login(String num, String pass) throws RemoteException {
if("admin".equals(num)&&"123456".equals(pass)){
return true;
}else{
return false;
}
}
@Override
public QQUser loginGetUser(Map map) throws RemoteException {
String num=map.get("num").toString();
String pass=map.get("pass").toString();
if("5201314".equals(num)&&"123456".equals(pass)){
return new QQUser(num,pass,"vampire","湖南","女","18");
}else{
return null;
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}
}
4.AndroidManifest.xml
<service android:name=".MyQQLoginService"
android:exported="true"
android:enabled="true"
>
</service>
5.QQUserOut.aidl
package com.zking.android25_myqq;
//定义QQUser
parcelable QQUser;
6.QQLoginOut.aidl
package com.zking.android25_myqq;
import com.zking.myqq.QQUserOut;
interface QQLoginOut {
boolean login(String num,String pass);
QQUser loginGetUser(in Map map);
}
7.MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText et_main_num;
private EditText et_main_pass;
private TextView tv_main_result;
private Intent intent;
private QQLoginOut qqLoginOut;
//初始化ServiceConnection
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("test","连接成功");
qqLoginOut = QQLoginOut.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i("test","连接失败");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initviews();
intent = new Intent(this,MyQQLoginService.class);
}
@Override
protected void onResume() {
super.onResume();
//绑定服务
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//和服务解除绑定
unbindService(connection);
}
private void initviews() {
et_main_num = (EditText) findViewById(R.id.et_main_num);
et_main_pass = (EditText) findViewById(R.id.et_main_pass);
tv_main_result = (TextView) findViewById(R.id.tv_main_result);
}
public void login(View view){
String num=et_main_num.getText().toString();
String pass=et_main_pass.getText().toString();
//连接后台
boolean b= false;
try {
Map map=new HashMap();
map.put("num",num);
map.put("pass",pass);
QQUser qqUser= qqLoginOut.loginGetUser(map);
if(qqUser!=null){
tv_main_result.setText("雷好:"+qqUser.getNetName()+" 年芳:"+qqUser.getAge()+" 性别:"+qqUser.getSex());
}else{
tv_main_result.setText("用户名或密码错误");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}