Android编程实现AIDL(跨进程通信)的方法详解
程序员文章站
2024-03-03 15:13:10
本文实例讲述了android编程实现aidl(跨进程通信)的方法。分享给大家供大家参考,具体如下:
一. 概述:
跨进程通信(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操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。