Android 跨进程通信

ops/2024/9/23 10:51:01/

Android中常用的跨进程通信方法有以下几种:
Intent、Binder、AIDL、Messenger、ContentProvider。

Intent

可以通过Intent传递数据和消息,但是只能传递一些简单的数据类型,比如字符串、整数等。
示例:

  1. 从一个应用程序发送一个字符串到另一个应用程序,例如:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MainActivity"));
intent.putExtra("key", "value");
startActivity(intent);
  1. 从一个服务发送一个自定义类的对象到另一个服务,例如:
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(serviceConnection);
Bundle bundle = new Bundle();
bundle.putParcelable("key", myObject);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MyService"));
intent.putExtras(bundle);
sendBroadcast(intent);

Binder

是Android系统中的跨进程通信机制,它可以传递任意类型的对象,包括自定义类的对象。
示例:

  1. 在一个服务中创建一个对象,并将其绑定到另一个服务的Binder接口上,例如:
public class MyService extends Service {private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {@Overridepublic String getMessage() throws RemoteException {return "Hello from MyService";}};@Nullable@Overridepublic IBinder onBind(Intent intent) {return mBinder;}
}
  1. 在客户端应用程序中绑定到服务并调用其方法,例如:
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);try {String message = myAidlInterface.getMessage();} catch (RemoteException e) {e.printStackTrace();}}
});
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);

AIDL

是Android系统中的一种接口描述语言,用于定义服务端和客户端之间的接口。
示例同上。

Messenger

是一种轻量级的通信方式,可以在不同的进程之间传递消息。
示例:

  1. 在服务端创建一个Handler对象,并将其封装到一个Binder对象中,例如:
public class MyService extends Service {private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {@Overridepublic String getMessage() throws RemoteException {return "Hello from MyService";}};private final Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {Bundle bundle = msg.getData();String message = bundle.getString("message");Log.d(TAG, "Received message: " + message);}};@Nullable@Overridepublic IBinder onBind(Intent intent) {return mBinder;}
}
  1. 在客户端应用程序中绑定到服务并调用其方法,例如:
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);try {Message message = Message.obtain(null, 0, null, myAidlInterface.getMessage());myService.mHandler.sendMessage(message);} catch (RemoteException e) {e.printStackTrace();}}
});
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);

ContentProvider

是一种特殊的IContentObserver,用于在不同的应用程序之间共享数据。

以下是一个简单的示例:在主进程中创建一个ContentProvider,然后在子进程中通过ContentResolver访问这个ContentProvider来获取数据。

// 在主进程中注册ContentProvider
public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();ContentProvider myContentProvider = new MyContentProvider();registerContentProvider(myContentProvider);}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubreturn null;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubreturn null;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}
}
// 在子进程中通过ContentResolver访问ContentProvider获取数据
public class MyActivity extends Activity {private static final String AUTHORITY = "com.example.myapp.provider";private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Cursor cursor = getContentResolver().query(BASE_CONTENT_URI, null, null, null, null);while (cursor.moveToNext()) {// TODO: Do something with the data in the cursor.}cursor.close();}
}

http://www.ppmy.cn/ops/21241.html

相关文章

【紫光同创盘古PGX-Lite 7K教程】——(盘古PGX-Lite 7K开发板/PGC7KD-6IMBG256第七章)数字钟实验例程

本原创教程由深圳市小眼睛科技有限公司创作,版权归本公司所有,如需转载,需授权并注明出处(www.meyesemi.com) 适用于板卡型号: 紫光同创PGC7KD-6IMBG256开发平台(盘古PGX-Lite 7K) 仅需一根Ty…

成为程序员后我们都明白了什么?

成为程序员后我们都明白了什么? 作为一名程序员,我获得了很多收获和体会。首先,我发现编程是一项极具挑战性和创造性的工作,让我可以不断学习和成长。通过解决问题和构建应用程序,我可以看到自己的成果,这…

防火墙技术基础篇:认识安全策略、安全区域、域间转发及报文转发流程

防火墙技术基础篇:认识安全策略、安全区域、域间转发及报文转发流程 一、安全策略匹配机制 简单通俗的讲,防火墙设备最基本的用途就是定义数据如何转发,靠什么定义呢?最基本的就是安全策略,当流量来到防火墙之后首先…

【Linux】MySQL的安装及配置(Ubuntu-18.04)

一、安装MySQL 分别安装MySQL服务器、MySQL客户端、C/C开发库 sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install libmysqlclient-dev 二、配置MySQL 1.查看默认配置文件,此处的user和password为默认提供的,…

什么是vue,vue怎样使用?

Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面&#xff0…

设计模式:单例、原型和生成器

在这篇文章中,我们将重点介绍其余的创建模式:Singleton,Builder和Prototype。 在我看来,这些模式不如工厂重要。然而,了解它们仍然很有用。我将提供UML描述,简单的java示例(这样即使你不了解jav…

unittest自动化测试框架讲解以及实战

为什么要学习unittest 按照测试阶段来划分,可以将测试分为单元测试、集成测试、系统测试和验收测试。单元测试是指对软件中的最小可测试单元在与程序其他部分相隔离的情况下进行检查和验证的工作,通常指函数或者类,一般是开发完成的。 单元…

Ubuntu-22.04.4安装问题

查看linux版本 uname -a5.15.0-71-generic #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux5.15.0-71-generic 内核版本#78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 编译内核时间x86_64 x86_64 x86_64 GNU/Linux安装 默认安装ssh 可选默认安装…