借助AIDL能够实现进程间的通信,记录一下如何使用;
1.创建一个AIDL文件,这里命名ICalAIDL:
package com.test.android.test;
// Declare any non-default types here with import statements
interface ICalAIDL {
int add(int x, int y);
int sub(int x, int y);
}
如果是AndroidStudio build一下项目,就会在app/build/generated/source/aidl/debug下自动生成一个ICalAIDL.java文件。
2.服务端代码
public class CalService extends Service {
private static final String TAG = "CalService";
private ICalAIDL.Stub mBinder = new ICalAIDL.Stub() {
@Override
public int add(int x, int y) throws RemoteException {
return x + y;
}
@Override
public int sub(int x, int y) throws RemoteException {
return x - y;
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return mBinder;
}
}
创建一个ICalAIDL.Stub对象,该类在自动生成的ICalAIDL.java文件中。重写定义的接口的两个方法,这两个方法就是我们创建的AIDL文件内的2个方法。Stub这个类是Binder的子类,在onBind()方法中返回该对象。最后注册这个Service,当前让它运行在独立的进程中。
3.客户端代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button mBindService;
private Button mUnBindService;
private Button mAdd;
private Button mSub;
private ICalAIDL mICalAIDL;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected");
mICalAIDL= ICalAIDL.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
mICalAIDL = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBindService = (Button) findViewById(R.id.bindService);
mUnBindService = (Button) findViewById(R.id.unbindService);
mAdd = (Button) findViewById(R.id.add);
mSub = (Button) findViewById(R.id.sub);
mBindService.setOnClickListener(this);
mUnBindService.setOnClickListener(this);
mAdd.setOnClickListener(this);
mSub.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bindService: {
Intent intent = new Intent(MainActivity.this, CalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
break;
}
case R.id.unbindService: {
unbindService(mConnection);
break;
}
case R.id.add: {
if(mICalAIDL != null) {
try {
int addAns = mICalAIDL.add(1, 2);
Toast.makeText(MainActivity.this, "1 + 2 = " + addAns, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
Log.i(TAG, e.toString());
}
} else {
Toast.makeText(MainActivity.this, "连接服务器异常,请重新连接", Toast.LENGTH_SHORT).show();
}
break;
}
case R.id.sub: {
if(mICalAIDL != null) {
try {
int subAns = mICalAIDL.sub(30, 12);
Toast.makeText(MainActivity.this, "30 - 12 = " + subAns, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
Log.i(TAG, e.toString());
}
} else {
Toast.makeText(MainActivity.this, "连接服务器异常,请重新连接", Toast.LENGTH_SHORT).show();
}
break;
}
default:
break;
}
}
}
创建ServiceConnection对象,重写2个方法,在onServiceConnected()方法内通过IBinder获得ICalAIDL对象。之后就可以调用ICalAIDL对象的方法了。
这里记录一下如何实现进程间通信,原理并不是很懂~