Android编程实现AIDL(跨进程通信)的方法详解
作者:zeo 发布时间:2022-08-29 00:19:23
标签: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程序设计有所帮助。


猜你喜欢
- 年前无意看到一个用Python写的小桌面程序,可以自动玩扫雷的游戏,觉得挺有意思,决定用C#也做一个。【真实情况
- 介绍MyBatis 允许你在映射语句执行过程中的某一点进行拦截调用。比如执行前、执行后或者对SQL结果集处理、sql入参处理等,这样就可以在
- 概念 在 HTML 中,<a>, <form>, <img>, <script>,
- 前言前一篇文章我们熟悉了HikariCP连接池,也了解到它的性能很高,今天我们讲一下另一款比较受欢迎的连接池:Druid,这是阿里开源的一款
- 本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等。 在下面的描述中,将以&
- // 获取国家省市区信息$(document).ready(function(){//从程序
- 本文通过优化买票的重复流程来说明享元模式,为了加深对该模式的理解,会以String和基本数据类型的包装类对该模式的设计进一步说明。读者可以拉
- Java是面向对象的编程语言,在我们开发Java应用的程序员的专业术语里,Java这个单词其实指的是Java开发工具,也就是JDK(Java
- 夏天到了、小雪来给大家降降温话不多说、直接进入主题主要功能模块设计:登录注册、首页信息浏览、选课分类查看、选课详情查看、评论交流、收藏、浏览
- 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> &nbs
- 本文实例为大家分享了C#实现学生档案查询的具体代码,供大家参考,具体内容如下using System;using System.Collec
- 题目描述:给定一 m*n 的矩阵,请按照逆时针螺旋顺序,返回矩阵中所有元素。示例:思路:这是一道典型的模拟问题:我们可以分析一下,遍历前进轨
- using System; using System.IO; using System.Data; using System.Text; u
- 之前有做过手机端后台的国际化,因为手机统一传递了language参数所以只要设置LocaleChangeInterceptor就行了/**
- 1:Group的功能Group可以管理一组节点Group可以对管理的节点进行增删改查的操作Group可以管理节点的属性1.2:看看JDKSE
- 本文实例讲述了C#命令模式。分享给大家供大家参考。具体实现方法如下:using System;using System.Collection
- 找了很久查询objectid的方法都是错的,用mongovue能查询出来,但就是用java不知道怎么查询1.mongovue里的查询方式:{
- 错误处理到目前为止,我们都没怎么介绍onComplete()和onError()函数。这两个函数用来通知订阅者,被观察的对象将停止发送数据以
- 前言Mybatis是web工程开发中非常常用的数据持久化的框架,通过该框架,我们非常容易的进行数据库的增删改查。数据库连接进行事务提交的时候
- 记得当初自己刚开始学习Java的时候,对Java的IO流这一块特别不明白,所以写了这篇随笔希望能对刚开始学习Java的人有所帮助,也方便以后