adil,跨进程通信,一个简单的例子,来理解一下adil。
创建一个项目,名字随意,既然要跨进程,我们就创建一个service ,依照onprocess方法让他运行在一个单独的线程中
<service
android:process=":service_new"
android:name=".RemoteService"
android:enabled="true"
android:exported="true">
</service>
这里名字取为RemoteService,也是最原始的方法,通过这个绑定方法返回一个adil接口实例的方法来实现。
首先我们先创建一个adil文件
名字默认为IMyAidlInterface,创建好了过后如图所示
里面有一个默认的方法,这个方法就是我们要进行操作的方法。
修改为我们想要的方法,简单的完成a+b,我们就修改为
package com.example.jb.aidl;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int add(int num,int num2);
}
接口实现了,那我们就要在代码中实现交互,在service中我们将其实例化出来
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class RemoteService extends Service {
public RemoteService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IBinder(); //绑定过后返回实例
}
private class IBinder extends IMyAidlInterface.Stub { //通过继承IMyAidlInterface.Stub来实现其中的方法
@Override
public int add(int num, int num1) throws RemoteException {
return num + num1;
}
};
}
接下来既然已经在service中实现了方法,那我们的方法就是在activity中拿到该实例同时进行加减操作
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private String TAG="MainActivity";
private Button toAdd;
IMyAidlInterface iMyAidlInterface;
private ServiceConnection conn = new ServiceConnection() {
//绑定上服务的时候
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//接受到了远程的服务
Log.e(TAG, "绑定成功");
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
// 当服务断开的时候调用
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e(TAG, "绑定丢失");
//回收资源
iMyAidlInterface = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindServce();
}
private void initView() {
toAdd = (Button) findViewById(R.id.toAdd);
toAdd.setOnClickListener(this);
}
@Override
public void onClick(View view) {
try {
//调用远程服务
int valueRes = iMyAidlInterface.add(1, 2);
Log.e(TAG, valueRes+"");
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void bindServce() {
//获取到服务端, 5.0 之后必须显示绑定服务
Intent intent = new Intent(this,RemoteService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
Log.e(TAG, "bindServce: bind on end");
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
布局代码很简单就一个button,当每一次点击button都会实现1+2的运算,同时在控制台打印出来。
一个简简单单的model实现了service和activity的通信,他们在不同的进程中。
大家可能会问,不就是绑定了 service吗,跨什么进程,我也 试了一下,不通过aidl文件直接绑定service,程序会直接崩溃,毕竟他们都不属于同一个进程,常规的bindservice肯定是行不通的。
以上有任何 问题欢迎指正。