AIDL跨进程通信

时间:2021-04-30 02:51:31

Android跨进程通信会用到AIDL,当然跨进程通信不一定要用AIDL,像广播也是可以的,当然这里用到AIDL相对比较安全一些;

AIDL允许传递基本数据类型(Java 的原生类型如int/long/char/boolean/double/float/String、CharSequence、List和Map、)、实现android.os.Parcelable 接口的对象类

两个实例来验证一下AIDL

1.创建一个提供AIDL服务的AIDL_Test_Server,新建AIDL接口IRemoteService,在接口中定义方法

package cn.jsonlu.aidl.server;

/**
*远程AIDL服务接口
**/
interface IRemoteService { /**
*定义接口方法
*/
int add(int a,int b);
}

2.在AIDL_Test_Server中创建服务

package cn.jsonlu.aidl.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; /**
* Author:JsonLu
* DateTime:2016/2/25 10:59
* Email:jsonlu@qq.com
* Desc:
**/
public class RemoteService extends Service { private IBinder iBinder = new IRemoteService.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}; @Override
public IBinder onBind(Intent intent) {
return iBinder;
}
}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.jsonlu.aidl.server"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".RemoteService"
android:exported="true">
<intent-filter>
<action android:name="cn.jsonlu.aidl.server.IRemoteService" />
</intent-filter>
</service>
</application> </manifest>

3.创建一个使用AIDL服务的AIDL_Test_Client,将AIDL_Test_Server中的.aidl文件复制到AIDL_Test_Client中(接口包名必须一致)

package cn.jsonlu.aidl.client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.EditText; import cn.jsonlu.aidl.server.IRemoteService; public class MainActivity extends Activity { private EditText a, b, c;
private IRemoteService aidl;
private boolean bindFlag = false;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
aidl = IRemoteService.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) {
aidl = null;
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bindService();
} private void init() {
a = (EditText) findViewById(R.id.num_a);
b = (EditText) findViewById(R.id.num_b);
c = (EditText) findViewById(R.id.num_c);
} public void onClick(View v) {
if (!bindFlag) {
bindService();
} else {
int sum = 0;
try {
sum = aidl.add(Integer.parseInt(a.getText().toString()), Integer.parseInt(b.getText().toString()));
} catch (RemoteException e) {
System.out.println("AIDL错误");
}
c.setText(String.valueOf(sum));
}
} private void bindService() { Intent intent = new Intent(IRemoteService.class.getName());
intent.setClassName("cn.jsonlu.aidl.server", "cn.jsonlu.aidl.server.RemoteService");
bindFlag = bindService(intent, conn, BIND_AUTO_CREATE);
if (!bindFlag) {
System.out.println("AIDL未绑定成功");
} /*
//打开其他APP页面
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("cn.jsonlu.aidl.server", "cn.jsonlu.aidl.server.MainActivity");
intent.setComponent(cn);
startActivity(intent);
*/ }
}

4.AIDL传递对象需要实现Parcelable接口

AIDL跨进程通信

新建Bean实体类

package cn.jsonlu.aidl.server;

import android.os.Parcel;
import android.os.Parcelable; /**
* Author:JsonLu
* DateTime:2016/2/25 13:33
* Email:jsonlu@qq.com
* Desc:
**/
public class Person implements Parcelable { private int age;
private String name; public Person(int age, String name) {
this.age = age;
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} protected Person(Parcel in) {
readFromParcel(in);
} public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
} @Override
public Person[] newArray(int size) {
return new Person[size];
}
}; @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
} //注意读取变量和写入变量的顺序应该一致 不然得不到正确的结果
public void readFromParcel(Parcel source) {
name = source.readString();
age = source.readInt();
}
}

新建Person.aidl文件

// Person.aidl
package cn.jsonlu.aidl.server;
parcelable Person;

修改IRemoteService.aidl文件

// IRemoteService.aidl
package cn.jsonlu.aidl.server;
import cn.jsonlu.aidl.server.Person;
// Declare any non-default types here with import statements interface IRemoteService {
//计算两数的和
int add(int a,int b);
//传递对象
Person getPerson();
}