什么是AIDL:Android Interface definition language,android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
有什么用途:用于不同进程中数据的交换
实现步骤:
1.在com.aidl包下建立 IMyAidl.aidl

.aidl文件的内容与Java代码非常相似,但要注意,不能加修饰符(例如,public、private)、AIDL服务不支持的数据类型(例如,InputStream、OutputStream)等内容。

package com.aidl;interface IMyAidl{String getValue();}

2.编译后在gen下生成了对应的.java文件

3.编写一个service,好像aidl都要配合service来使用,里面实现了IMyAidl.Stub

public class MyService extends Service{public class MyAidl extends IMyAidl.Stub{@Overridepublic String getValue() throws RemoteException {// TODO Auto-generated method stubreturn "aidl test";}}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MyAidl();}}

配置service

 <service android:name=".MyService">        <intent-filter >        <action android:name="com.aidl.IMyAidl"/>        </intent-filter>        </service>

4.在activity通过bindService(Intent service, ServiceConnection conn, int flags)方法绑定服务,得到IMyAidl,调用getValue(),这样activity就可以得到从service传来的值了。

public class AIDLTestActivity extends Activity {private IMyAidl aidl;private TextView tx; private  ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 通过服务获得里面的定义的MyAidl类  aidl = IMyAidl.Stub.asInterface(service);}};    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tx = (TextView)findViewById(R.id.tx);        Button bt1 = (Button)findViewById(R.id.bt1);        Button bt2 = (Button)findViewById(R.id.bt2);        bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//绑定服务bindService(new Intent("com.aidl.IMyAidl"), conn, Context.BIND_AUTO_CREATE);}});        bt2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (aidl!=null) {try {//通过IMyAidl得到service传来的值tx.setText(aidl.getValue());} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}});    }}

总结: 这里简单练习下,在静默安装和拦截电话都用到aidl,有空再写这些例子练习了。

更多相关文章

  1. [Android]ButterKnife-无尽之刃-绑定视图控件和事件的快速开发工
  2. Android5.0开关机模块——zygote进程
  3. Android Studio自动化快速实现Parcelable接口序列化
  4. Android 接口定义语言AIdl
  5. Android当前任务管理器不显示应用进程
  6. Android 进程间通信AIDL(一)
  7. php做接口+android 请求API接口并展示到ListView例子

随机推荐

  1. 意派Epub360丨元宵节借势营销H5,玩出创意,
  2. vue-resource ajax请求
  3. Vue项目i18n多语言国际化过程中,过滤器fil
  4. 使用云服务器,如何实现Rdesktop连接?
  5. 【北亚数据恢复】DELL EqualLogic PS6100
  6. Web Components系列(三) —— 创建 Custom
  7. 云起实验室本周推荐体验场景,免费云上学习
  8. 怎么画人物的头发?插画人物头发绘画技巧!
  9. 软路由openwrt中替换国内镜像源(以阿里云
  10. 如何画好动漫的眼睛?动漫眼睛画法教程