Android编程实现AIDL(跨进程通信)的方法详解

Android编程实现AIDL(跨进程通信)的方法详解,第1张

概述本文实例讲述了Android编程实现AIDL(跨进程通信)的方法。分享给大家供大家参考,具体如下:

本文实例讲述了AndroID编程实现AIDL(跨进程通信)的方法。分享给大家供大家参考,具体如下:

一. 概述:

跨进程通信(AIDL),主要实现进程(应用)间数据共享功能。

二. 实现流程:

1. 服务器端实现:

(1)目录结构,如下图:

(2)实现*.aIDl文件:

A. IAIDLService.aIDl实现:

package com.focus.aIDl;import com.focus.aIDl.Person;interface IAIDLService {  String getname();  Person getPerson();}

B. Person.aIDl实现:

parcelable Person;

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

package com.focus.aIDl;import androID.os.Parcel;import androID.os.Parcelable;public class Person implements Parcelable {  private String name;  private int age;  public Person() {  }  public Person(Parcel source) {    name = source.readString();    age = source.readInt();  }  public String getname() {    return name;  }  public voID setname(String name) {    this.name = name;  }  public int getAge() {    return age;  }  public voID setAge(int age) {    this.age = age;  }  public int describeContents() {    return 0;  }  public voID writetoParcel(Parcel dest,int flags) {    dest.writeString(name);    dest.writeInt(age);  }  public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {    public Person[] newArray(int size) {      return new Person[size];    }    public Person createFromParcel(Parcel source) {      return new Person(source);    }  };}

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

package com.focus.aIDl;import com.focus.aIDl.IAIDLService.Stub;import androID.app.Service;import androID.content.Intent;import androID.os.IBinder;import androID.os.remoteexception;public class AIDLServiceImpl extends Service {  @OverrIDe  public IBinder onBind(Intent intent) {    return mBinder;  }  /**   * 在AIDL文件中定义的接口实现。   */  private IAIDLService.Stub mBinder = new Stub() {    public String getname() throws remoteexception {      return "mayingcai";    }    public Person getPerson() throws remoteexception {      Person mPerson = new Person();      mPerson.setname("mayingcai");      mPerson.setAge(24);      return mPerson;    }  };}

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

<service androID:name = ".AIDLServiceImpl" androID:process = ":remote">  <intent-filter>    <action androID:name = "com.focus.aIDl.IAIDLService" />  </intent-filter></service>

2. 客户端实现:

(1)目录结构,如下图:

(2)将服务器端的IAIDLService.aIDl,Person.aIDl和Person.Java文件拷贝到本工程中,如上图所示:

(3)res/layout/main.xml实现:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID = "http://schemas.androID.com/apk/res/androID"  androID:orIEntation = "vertical"  androID:layout_wIDth = "fill_parent"  androID:layout_height = "fill_parent"  >  <TextVIEw    androID:ID = "@+ID/name"    androID:layout_wIDth = "wrap_content"    androID:layout_height = "wrap_content"    />  <button    androID:ID = "@+ID/connection"    androID:layout_wIDth = "wrap_content"    androID:layout_height = "wrap_content"    androID:text = "连接"    />  <button    androID:ID = "@+ID/message"    androID:layout_wIDth = "wrap_content"    androID:layout_height = "wrap_content"    androID:enabled = "false"    androID:text = "信息"    />  <button    androID:ID = "@+ID/person"    androID:layout_wIDth = "wrap_content"    androID:layout_height = "wrap_content"    androID:enabled = "false"    androID:text = "人"    /></linearLayout>

(4)主Activity实现,从服务器端获取数据在客户端显示:

package com.focus.aIDl.clIEnt;import androID.app.Activity;import androID.content.Componentname;import androID.content.Intent;import androID.content.ServiceConnection;import androID.os.Bundle;import androID.os.IBinder;import androID.os.remoteexception;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.TextVIEw;import com.focus.aIDl.IAIDLService;import com.focus.aIDl.Person;public class AIDLClIEntAcitivty extends Activity {  private IAIDLService mAIDLService;  private TextVIEw mname;  private button mMessage;  private button mPerson;  /**   * 第一步,创建ServiceConnection对象,在onServiceConnected()方法中获取IAIDLService实现。   */  private ServiceConnection mServiceConnection = new ServiceConnection() {    public voID onServiceConnected(Componentname name,IBinder service) {      mAIDLService = IAIDLService.Stub.asInterface(service);      mMessage.setEnabled(true);      mPerson.setEnabled(true);    }    public voID onServicedisconnected(Componentname name) {      mAIDLService = null;      mMessage.setEnabled(false);      mPerson.setEnabled(false);    }  };  @OverrIDe  public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    mname = (TextVIEw) findVIEwByID(R.ID.name);    findVIEwByID(R.ID.connection).setonClickListener(new OnClickListener() {      public voID onClick(VIEw vIEw) {        /**         * 第二步,单击"连接"按钮后用mServiceConnection去bind服务器端创建的Service。         */        Intent service = new Intent("com.focus.aIDl.IAIDLService");        bindService(service,mServiceConnection,BIND_auto_CREATE);      }    });    mMessage = (button) findVIEwByID(R.ID.message);    mMessage.setonClickListener(new OnClickListener() {      public voID onClick(VIEw vIEw) {        /**         * 第三步,从服务器端获取字符串。         */        try {          mname.setText(mAIDLService.getname());        } catch (remoteexception e) {          e.printstacktrace();        }      }    });    mPerson = (button) findVIEwByID(R.ID.person);    mPerson.setonClickListener(new OnClickListener() {      public voID onClick(VIEw vIEw) {        /**         * 第四步,从服务器端获取Person对象。         */        try {          Person mPerson = mAIDLService.getPerson();          mname.setText("姓名:" + mPerson.getname() + ",年龄:" + mPerson.getAge());        } catch (remoteexception e) {          e.printstacktrace();        }      }    });  }}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android数据库 *** 作技巧总结》、《Android编程之activity *** 作技巧总结》、《@L_404_2@》、《Android编程开发之SD卡 *** 作方法汇总》、《Android开发入门与进阶教程》、《Android资源 *** 作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程实现AIDL(跨进程通信)的方法详解全部内容,希望文章能够帮你解决Android编程实现AIDL(跨进程通信)的方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149129.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存