AIDL进程通信
AIDL 简介
AIDL,全称是Android Interface Define Language,即安卓接口定义语言,可以实现安卓设备中进程之间的通信(Inter Process Communication, IPC)。
步骤
服务端
在main文件夹下创建,新建adil文件记得重建。
在adil文件内添加自己需要的方法,用来处理数据。
interface Aidl {
//抽象方法
int add (int a,int b);
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
自定义一个IBinder,引出adil中的Stub对象,自动重写adil文件中的方法。
在Service的绑定onBind方法中返回binder。
在服务端创建Service类:
public class MyService extends Service {
public MyService() {
}
IBinder binder=new Aidl.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a+b;
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
在清单文件中注册,并向外提供出去。
exoirted 设置为true , 保证能被其他应用调用。
<!--把service提供出去,起个名-->
<service android:name=".MyService"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="ki"/>
</intent-filter>
</service>
客户端
在客户端要求和服务端一样的adil文件,可以复制到客户端。
客户端绑定服务:
public class MainActivity extends AppCompatActivity {
Aidl aidl;
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
aidl=Aidl.Stub.asInterface(iBinder);
int num=0;
try {
num=aidl.add(2,1);
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, " "+num, Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
//防止溢出,清空接口
aidl=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("ki");
intent.setPackage("com.example.day16_1");//服务端的包名
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
}
Application
自定义一个类,继承application:
public class MyApplaction extends Application {
String name="aaa";
@Override
public void onCreate() {
super.onCreate();
Log.i("TAG", "onCreate: 创建执行");
}
@Override
public void onTerminate() {
Log.i("TAG", "onTerminate: 程序终止时执行");
super.onTerminate();
}
@Override
public void onLowMemory() {
Log.i("TAG", "onLowMemory: 低内存时执行");
super.onLowMemory();
}
@Override
public void onTrimMemory(int level) {
Log.i("TAG", "onTrimMemory: 程序在内存清理时执行");
super.onTrimMemory(level);
}
@Override
public void onConfigurationChanged( Configuration newConfig) {
Log.i("TAG", "onConfigurationChanged: 设备发生变化时调用");
super.onConfigurationChanged(newConfig);
}
}
清单文件注册:
android:name=".MyApplaction"
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textview);
textView.setText(((MyApplaction) getApplication()).name);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//将application内的字符串修改
((MyApplaction) getApplication()).name="bbb";
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
Main2Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=findViewById(R.id.textview2);
textView.setText(((MyApplaction)getApplication()).name);
}