前言
距离上次玩AIDL的时候还是2年前,由于最近在看Android源代码,发现大量的AIDL的应用,于是重温了下,网上的文章大都讲得繁琐,可能是我搜索得方式不对,但是开发者得需求就是最短时间理解和应用,下面花几分钟快速掌握
第一步:理解
网上随便下载个Demo,https://github.com/gdutxiaoxu/AidlDemo
项目结构可以看出 client 和 server 两个得aidl得区别, 应该看得很明了吧,对!两份AIDL是一样的内容,所以知道怎么写了吧,server写完AIDL 直接复制一份到client里面
开始我也不信! 下面Compare用二进制对比验证,是不是豁然开朗,瞬间AIDL感觉不香了
第二步:应用
代码稍微有点复杂,在此基础上扩展倒是很方便,所以笔者简单讲解,建议自行下载跑一边
server
定义
<service
android:name=".PlayService"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="xj.musicserver.IPlayService"/>
</intent-filter>
</service>
public class PlayService extends Service {
private static final String TAG = "PlayService";
IPlayService.Stub mIPlayService=new IPlayService.Stub() {
@Override
public void play(String name, final IPlayListener iPlayListener) throws RemoteException {
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
LogUtil.i(TAG, "onBind: intent = " +intent.toString());
return mIPlayService;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
LogUtil.i(TAG, "onCreate: ");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
client
使用
public static final String ACTION = "xj.musicserver.IPlayService";
public static final String XJ_MUSICSERVER = "xj.musicserver";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parceable_demo);
}
private IPlayService mIPlayService;
ServiceConnection mServiceConnection=new ServiceConnection()
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIPlayService = IPlayService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
IPlayListener.Stub mPlayListener=new IPlayListener.Stub(){
@Override
public void onError(int code) throws RemoteException {
}
@Override
public void onSuccess(int code, MusicInfo musicInfo) throws RemoteException {
}
};
public void onButtonClick(View v) throws RemoteException {
switch (v.getId()){
case R.id.btn_contact:
if (mIPlayService != null && mIPlayService.asBinder().isBinderAlive()) {
try {
if(mIPlayService!=null){
mIPlayService.play("play", mPlayListener);
}
} catch (Exception e) {
Log.e(TAG, "Exception",e);
e.printStackTrace();
}
}
break;
case R.id.btn_start_service:
Intent intent = new Intent(ACTION);
intent.setPackage(XJ_MUSICSERVER);
bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
break;
}
}
PS:注意代码较老,做了点优化
1. 服务端断联
mIPlayService != null && mIPlayService.asBinder().isBinderAlive()
2. 权限
//android 7.0及以上需要动态申请权限
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}