一. 概述:
跨进程通信(AIDL),主要实现进程(应用)间数据共享功能。
二. 实现流程:
1. 服务器端实现:
(1)目录结构,如下图:



(2)实现*.aidl文件:
A. IAIDLService.aidl实现:


Java代码:

  1. import com.focus.aidl.Person;
  2. interface IAIDLService {
  3. String getName();
  4. Person getPerson();
  5. }
复制代码

B. Person.aidl实现:

parcelable Person;
(3)进程间传递对象必需实现Parcelable或Serializable接口,下面是被传递的Person对象实现:

Java代码:

  1. package eoe.demo;

  2. import android.os.Parcel;
  3. import android.os.Parcelable;

  4. public class Person implements Parcelable {

  5. private String name;
  6. private int age;

  7. public Person() {
  8. }

  9. public Person(Parcel source) {
  10. name = source.readString();
  11. age = source.readInt();
  12. }

  13. public String getName() {
  14. return name;
  15. }

  16. public void setName(String name) {
  17. this.name = name;
  18. }

  19. public int getAge() {
  20. return age;
  21. }

  22. public void setAge(int age) {
  23. this.age = age;
  24. }

  25. public int describeContents() {
  26. return
  27. 0;
  28. }

  29. public void writeToParcel(Parcel dest, int flags) {
  30. dest.writeString(name);
  31. dest.writeInt(age);
  32. }

  33. public static final Parcelable.Creator<;Person> CREATOR = new Creator<;Person>() {
  34. public Person[] newArray(int size) {
  35. return
  36. new Person[size];
  37. }

  38. public Person createFromParcel(Parcel source) {
  39. return
  40. new Person(source);
  41. }
  42. };

  43. }
复制代码


(4)实现IAIDLService.aidl文件中定义的接口,并定义Service,在Service被bind时返回此实现类:

Java代码:

  1. package eoe.demo;

  2. import com.focus.aidl.IAIDLService.Stub;
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.IBinder;
  6. import android.os.RemoteException;

  7. public class AIDLServiceImpl extends Service {

  8. @Override
  9. public IBinder onBind(Intent intent) {
  10. return mBinder;
  11. }

  12. /**
  13. * 在AIDL文件中定义的接口实现。
  14. */
  15. private IAIDLService.Stub mBinder = new Stub() {

  16. public String getName() throws RemoteException {
  17. return
  18. "mayingcai";
  19. }

  20. public Person getPerson() throws RemoteException {
  21. Person mPerson = new Person();

  22. mPerson.setName("mayingcai");
  23. mPerson.setAge(24);

  24. return mPerson;
  25. }

  26. };

  27. }
复制代码


(5)在AndroidManifest.xml文件中注册Service:

Java代码:

  1. <service android:name = ".AIDLServiceImpl" android:process = ":remote">

  2. <intent-filter>
  3. <action android:name = "com.focus.aidl.IAIDLService" />
  4. </intent-filter>

  5. </service>
复制代码

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. android 如何用代码生成圆角Bitmap图片
  7. Android创建目录文件夹,多级目录逐一创建
  8. 【Android习惯】文件、方法、变量命名规范参考(编辑中)
  9. Android实战教程第四篇之简单实现短信发送器

随机推荐

  1. Facebook Fresco体验 (一)
  2. Android手指绘图(一)
  3. linux kernel / android debug notes
  4. Android(安卓)SQLite Database and Conte
  5. Android:Creating Your Own Spelling Chec
  6. android sd card porting
  7. Part 2: Multi-threading-android-apps-m
  8. android notification应用
  9. Camera类实现3d动画(android)
  10. android 实时麦克风变化