AIDL ( Android Interface Definition Language) 是一种IDL 语言,用于生成可以在 Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。本文简单介绍AIDL的使用。

1.新建IRemoteService.aidl

view source print ? 1. packagecom.tang.remoteservicedemo; 2. interfaceIRemoteService { 3. String getInfo(); 4. } 从内容中也可以看出,这东西类似一个接口。既然定义了这么一个玩意,那么我们就要去实现它。

2.新建IService“实现”IRemoteService“接口”

view source print ? 01. packagecom.tang.remoteservicedemo; 02. 03. importjava.util.Date; 04. 05. importandroid.app.Service; 06. importandroid.content.Intent; 07. importandroid.os.IBinder; 08. importandroid.os.RemoteException; 09. 10. publicclass IService extendsService 11. { 12. privateIBinder iBinder = new IRemoteService.Stub() { 13. @Override 14. publicString getInfo() throws RemoteException { 15. // TODO Auto-generated method stub 16. returnnew Date(System.currentTimeMillis()).toLocaleString()+" 来自远程服务的信息!"; 17. } 18. }; 19. @Override 20. publicIBinder onBind(Intent intent) { 21. // TODO Auto-generated method stub 22. returniBinder; 23. } 24. } 基于Binder的不同进程间通信,Client与Service在不同的进程中,对用户程序而言当调用Service返回的IBinder接口后,访问Service中的方法就如同调用自己的函数一样。

3.配置IService供其他进程调用

view source print ? 1. <service android:name="com.tang.remoteservicedemo.IService" 2. android:process=":remote"> 3. <intent-filter> 4. <action android:name="com.tang.remoteservicedemo.IService"/> 5. </intent-filter> 6. </service>
配置一个所属进程名和一个action。

通过前面三个步骤,这个含有getInfo()方法的service就可以给别人调用了,下面在客户端调用它

4.新建一个ClientDemo工程将含有IRemoteService.aidl的那个包全部拷贝到src下,只留下aidl文件,其他全删除。

5.新建MainActivity,其他就是绑定service的操作了

view source print ? 001. packagecom.tang.clientdemo; 002. 003. importjava.util.Timer; 004. importjava.util.TimerTask; 005. 006. importcom.tang.remoteservicedemo.IRemoteService; 007. 008. importandroid.app.Activity; 009. importandroid.content.ComponentName; 010. importandroid.content.Context; 011. importandroid.content.Intent; 012. importandroid.content.ServiceConnection; 013. importandroid.os.Bundle; 014. importandroid.os.IBinder; 015. importandroid.os.RemoteException; 016. importandroid.util.Log; 017. 018. 019. publicclass MainActivity extendsActivity { 020. 021. privateIRemoteService iService = null; 022. privateboolean isBinded =false; 023. 024. ServiceConnection conn =new ServiceConnection() { 025. 026. @Override 027. publicvoid onServiceDisconnected(ComponentName name) { 028. // TODO Auto-generated method stub 029. isBinded =false; 030. iService =null; 031. } 032. 033. @Override 034. publicvoid onServiceConnected(ComponentName name, IBinder service) { 035. // TODO Auto-generated method stub 036. iService = IRemoteService.Stub.asInterface(service); 037. isBinded =true; 038. } 039. }; 040. 041. publicvoid doBind() 042. { 043. Intent intent =new Intent("com.tang.remoteservicedemo.IService"); 044. bindService(intent, conn, Context.BIND_AUTO_CREATE); 045. } 046. 047. publicvoid doUnbind() 048. { 049. if(isBinded) 050. { 051. unbindService(conn); 052. iService =null; 053. isBinded =false; 054. } 055. } 056. @Override 057. protectedvoid onCreate(Bundle savedInstanceState) { 058. super.onCreate(savedInstanceState); 059. setContentView(R.layout.activity_main); 060. newThread(new Runnable() { 061. @Override 062. publicvoid run() { 063. // TODO Auto-generated method stub 064. doBind(); 065. } 066. }).start(); 067. Timer timer =new Timer(); 068. timer.schedule(task,0, 2000); 069. 070. } 071. TimerTask task =new TimerTask() { 072. @Override 073. publicvoid run() { 074. // TODO Auto-generated method stub 075. if(iService!=null) 076. { 077. try{ 078. Log.i("AAA", iService.getInfo()); 079. }catch (RemoteException e) { 080. // TODO Auto-generated catch block 081. e.printStackTrace(); 082. } 083. } 084. else 085. { 086. Log.i("AAA","iService!=null"); 087. } 088. 089. } 090. }; 091. 092. @Override 093. protectedvoid onDestroy() { 094. // TODO Auto-generated method stub 095. doUnbind(); 096. task.cancel(); 097. super.onDestroy(); 098. } 099. }
执行之后的Log如下:

为什么会有一个为空的Log呢,因为绑定也是要时间的嘛....

更多相关文章

  1. Android(安卓)组件之Service解析
  2. Android(安卓)软键盘弹出时,EditText固定在键盘上方
  3. android6.0源码分析之AMS服务源码分析
  4. Android(安卓)Http协议访问网络
  5. Android(安卓)studio3.0上运行opencv3.2.0自带人脸识别实例Demo
  6. Android(安卓)SurfaceFlinger服务启动过程源码分析
  7. android weiget 实时数据更新
  8. android调用ITelephony类,AIDL实现电话,联系人黑名单拦截挂断服务
  9. Android初学笔记(记录自己的学习过程,有不对的地方欢迎指出)

随机推荐

  1. android studio配置系列 - 收藏集 - 掘金
  2. 使用delphi 开发多层应用(十三)使用Basic4a
  3. (转载)Android下Affinities和Task
  4. Android中称为四大组件
  5. activity 生命周期
  6. 开源阅读器FBReader Android版本的编译
  7. android瀑布流
  8. android bionic缺失pthread_cancel的解决
  9. Android四大组件之Activity---生命周期那
  10. Android(安卓)3D旋转动画之Camera 和 Mat