Android studio AIDL详解
2016-01-12 14:18
585 查看
[code] Android studio AIDL详解
之前用eclipse做远程service建立AIDL通信时,感觉挺简单的,但转到Android studio 遇到各种麻烦,下面我就详细讲各个流程,当然知识点我就不多讲了,因为相信大家都会,着重讲一些细节,流程.有不足的地方希望大家指正:
建立AIDL 并生成AIDL的java文件:
http://www.linuxidc.com/Linux/2015-01/111148.htm
建立服务端接口和相关代码以及注意事项:
客户端的建立与实现:
建立客户端我想提醒大家一句,由于基于AIDL,所以客户端的包名也要一样哦.
那么怎么用Android studio 建立客户端呢?
(1)
(2)
点击next进入下步,创建一个module.那为什么会是这样呢?
建好这个module后,就开始编码了:
(1)客户端接口,跟服务器一样
(2)客户端调用服务器代码:
public class MainActivity extends Activity {
Button button;
EditText editText_color,editText_weight;
ICat iCatClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
Intent intent=new Intent(“com.dzrplus.administrator.myclient.action.MyService”);
intent.setPackage(“com.dzrplus.administrator.myserviceaidl”);
bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
editText_color.setText(iCatClient.getColor());
editText_weight.setText(iCatClient.getWeight()+”“);
} catch (RemoteException e) {
System.out.println(“===============”+e.toString());
}
}
});
editText_color= (EditText) findViewById(R.id.et_color);
editText_weight=(EditText)findViewById(R.id.et_weight);
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iCatClient= ICat.Stub.asInterface(service);
}
[code]@Override public void onServiceDisconnected(ComponentName name) { }
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
}
注意看上面代码红色的部分,为什么要用 intent.setPackage(getPackageName());这个函数呢,其实是Android 5.0以上不支持Intent隐式启动方式,这只是一种解决方法,而google官方推荐使用的解决方法为下:
[code]public static Intent getExplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }
调用方式如下:
Intent mIntent = new Intent();
mIntent.setAction(“XXX.XXX.XXX”);
Intent eintent = new Intent(getExplicitIntent(mContext,mIntent));
context.startService(eintent);
5.接下来,你是不是很想跑一跑,但很遗憾你还是跑步起来的:一般会出现如下错误:
Failure [INSTALL_FAILED_OLDER_SDK]
那怎么解决呢,其实就是配置文件的问题,方案如下:
6.你是不是又想run一下,不好意思,还是会出问题:很简单,你这样每次run时,两个module只会相互覆盖,需要改如下配置:
先看看客户端的:
咱们再看看服务端的:
在这里你也知道为 intent.setPackage(“com.dzrplus.administrator.myserviceaidl”);
中的字符串为什么这么写了.
7.你是不是想问,现在run没问题了,我很想告诉你,的确是这样的,但现实呢,当然行.(冷笑话)
想下源码点击一下链接:http://download.csdn.net/detail/jimtrency/9400693
注:这只是个Demo,当然你不用创建两个module,直接两个peojiect也一样,不要给自己带来误区。记住,就是进程间通信
相关文章推荐
- android 禁用和开启四大组件的方法(setComponentEnabledSetting )?
- 史上最全Android Studio快捷键 汇总
- Android4.4[原生Settings]隐藏apk显示
- Android pull解析xml数据代码
- ANDROID颜色设置
- Android调用系统相机或相册更换头像
- Activity和Fragment的完整生命周期图
- android 从Activity 获取 rootView 根节点
- Android数字签名解析(二)
- Android Studio设置
- AndriodStudio集成友盟社会化分享5.0SDK出现的问题
- [转载]Android Native反调试
- android 系统默认值的设置
- Html和Android之间的联系
- Android API 中文 (3) —— 停止和重启一个Activity
- 关于android.view.WindowLeaked异常的解决方案
- Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery
- Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery
- Android中的so挂钩(hook)之替换Got表
- android wearable-Adding Voice Capabilities