已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
经验分享 Android中AIDL实现(跨进程通信,有源码)本帖最后由 Mr jing 于 2011-5-3 15:46 编辑 一. 概述: 跨进程通信(AIDL),主要实现进程(应用)间数据共享功能。二. 实现流程: 1. 服务器端实现: (1)目录结构,如下图: 下载 (20.96 KB)2011-5-3 04:56 (2)实现*.aidl文件: A. IAIDLService.aidl实现:view plaincopy to clipboardprint?1. package com.focus.aidl; 2.3. import com.focus.aidl.Person; 4.5. interface IAIDLService 6.7. String getName(); 8.9. Person getPerson(); 10.11. B. Person.aidl实现:view plaincopy to clipboardprint?1. parcelable Person; (3)进程间传递对象必需实现Parcelable或Serializable接口,下面是被传递的Person对象实现:view plaincopy to clipboardprint?1. package com.focus.aidl; 2.3. import android.os.Parcel; 4. import android.os.Parcelable; 5.6. publicclass Person implements Parcelable 7.8. private String name; 9.10. privateint age; 11.12. public Person() 13.14. 15.16. public Person(Parcel source) 17. name = source.readString(); 18. age = source.readInt(); 19. 20.21. public String getName() 22. return name; 23. 24.25. publicvoid setName(String name) 26. = name; 27. 28.29. publicint getAge() 30. return age; 31. 32.33. publicvoid setAge(int age) 34. this.age = age; 35. 36.37. publicint describeContents() 38. return0; 39. 40.41. publicvoid writeToParcel(Parcel dest, int flags) 42. dest.writeString(name); 43. dest.writeInt(age); 44. 45.46. publicstaticfinal Parcelable.Creator CREATOR = new Creator() 47. public Person newArray(int size) 48. returnnew Personsize; 49. 50.51. public Person createFromParcel(Parcel source) 52. returnnew Person(source); 53. 54. ; 55.56. (4)实现IAIDLService.aidl文件中定义的接口,并定义Service,在Service被bind时返回此实现类:view plaincopy to clipboardprint?1. package com.focus.aidl; 2.3. import com.focus.aidl.IAIDLService.Stub; 4.5. import android.app.Service; 6. import android.content.Intent; 7. import android.os.IBinder; 8. import android.os.RemoteException; 9.10. publicclass AIDLServiceImpl extends Service 11.12. Override 13. public IBinder onBind(Intent intent) 14. return mBinder; 15. 16.17. /* 18. * 在AIDL文件中定义的接口实现。 19. */ 20. private IAIDLService.Stub mBinder = new Stub() 21.22. public String getName() throws RemoteException 23. returnmayingcai; 24. 25.26. public Person getPerson() throws RemoteException 27. Person mPerson = new Person(); 28.29. mPerson.setName(mayingcai); 30. mPerson.setAge(24); 31.32. return mPerson; 33. 34.35. ; 36.37. (5)在AndroidManifest.xml文件中注册Service:view plaincopy to clipboardprint?1. 2.3. 4. 5. 6.7. 2. 客户端实现: (1)目录结构,如下图: 下载 (21.6 KB)2011-5-3 04:56 (2)将服务器端的IAIDLService.aidl,Person.aidl和Person.java文件拷贝到本工程中,如上图所示: (3)res/layout/main.xml实现:view plaincopy to clipboardprint?1. 2.3. 8.9. 14.15. 21.22. 29.30. 37.38. (4)主Activity实现,从服务器端获取数据在客户端显示:view plaincopy to clipboardprint?1. package com.focus.aidl.client; 2.3. import android.app.Activity; 4. import android.content.ComponentName; 5. import android.content.Intent; 6. import android.content.ServiceConnection; 7. import android.os.Bundle; 8. import android.os.IBinder; 9. import android.os.RemoteException; 10. import android.view.View; 11. import android.view.View.OnClickListener; 12. import android.widget.Button; 13. import android.widget.TextView; 14.15. import com.focus.aidl.IAIDLService; 16. import com.focus.aidl.Person; 17.18. publicclass AIDLClientAcitivty extends Activity 19.20. private IAIDLService mAIDLService; 21.22. private TextView mName; 23.24. private Button mMessage; 25.26. private Button mPerson; 27.28. /* 29. * 第一步,创建ServiceConnection对象,在onServiceConnected()方法中获取IAIDLService实现。 30. */ 31. private ServiceConnection mServiceConnection = new ServiceConnection() 32.33. publicvoid onServiceConnected(ComponentName name, IBinder service) 34. mAIDLService = IAIDLService.Stub.asInterface(service); 35.36. mMessage.setEnabled(true); 37. mPerson.setEnabled(true); 38. 39.40. publicvoid onServiceDisconnected(ComponentName name) 41. mAIDLService = null; 42.43. mMessage.setEnabled(false); 44. mPerson.setEnabled(false); 45. 46.47. ; 48.49. Override 50. publicvoid onCreate(Bundle savedInstanceState) 51. super.onCreate(savedInstanceState); 52.53. setContentView(R.layout.main); 54.55. mName = (TextView) findViewById(R.); 56.57. findViewById(R.id.connection).setOnClickListener(new OnClickListener() 58. publicvoid onClick(View view) 59.60. /* 61. * 第二步,单击连接按钮后用mServiceConnection去bind服务器端创建的Service。 62. */ 63. Intent service = new Intent(com.focus.aidl.IAIDLService); 64. bindService(service, mServiceConnection, BIND_AUTO_CREATE); 65.66. 67. ); 68.69. mMessage = (Button) findViewById(R.id.message); 70. mMessage.setOnClickListener(new OnClickListener() 71. publicvoid onClick(View view) 72. /* 73. * 第三步,从服务器端获取字符串。 74. */ 75. try 76. mName.setText(mAIDLService.getName(); 77. catch (RemoteException e) 78. e.printStackTrace(); 79. 80. 81. ); 82.83. mPerson = (Button) findViewById(R.id.person); 84. mPerson.setOnClickListener(new OnClickListener() 85. publicvoid onClick(View view) 8
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年大学《动物药学-兽用生物制品学》考试备考试题及答案解析
- 2025年大学《香料香精技术与工程-香精调配技术》考试备考试题及答案解析
- 2025年大学《傣医学-傣医方剂学》考试备考试题及答案解析
- 借助资料理解课文主要内容教学设计-2025-2026学年统编版小初衔接-统编版(小初衔接)
- 社会和心理评估
- 2025年大学《生物工程-酶工程》考试备考试题及答案解析
- 2025年大学《外交学-外交学原理》考试备考题库及答案解析
- 2025年大学《环境生态工程-生态影响评价(项目生态风险评估)》考试备考试题及答案解析
- 2025年大学《化学生物学-无机化学》考试备考试题及答案解析
- 2025年大学《融合教育-融合教育课程设计》考试备考题库及答案解析
- 2025下半年榆林神木市公共服务辅助人员招聘(80人)考试笔试备考试题及答案解析
- 腾讯手机行业消费趋势洞察报告(2025年版)
- AIGC艺术设计 课件全套 第1-8章 艺术设计的新语境:AI的介入 -AIGC艺术设计的思考与展望
- 个人所得税APP培训课件
- 卡车基本构造专业知识课件
- 教学成果奖申报技巧课件
- 部编版道德与法治五年级上册【第四单元】全单元课件
- 取、弃土场作业指导书
- 基桩完整性试验检测记录表(低应变法)
- 2023学年安徽省合肥市一六八中学物理高二第一学期期中监测试题含解析
- 居住型公寓设计要求及标准(68页)
评论
0/150
提交评论