[经验分享] Android中AIDL实现(跨进程通信.docx_第1页
[经验分享] Android中AIDL实现(跨进程通信.docx_第2页
[经验分享] Android中AIDL实现(跨进程通信.docx_第3页
[经验分享] Android中AIDL实现(跨进程通信.docx_第4页
[经验分享] Android中AIDL实现(跨进程通信.docx_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论