什么时候应该使用每个Android的不同消息类型的?什么时候、不同、类型、消息

2023-09-04 12:08:51 作者:总有一天你会明白我的爱

我已经工作了Android远远超过一年了,但我仍然有麻烦确定何时/线程应该使用不同类型的进程间通信/通讯。我主要讲的是广播的意图,使用AIDL的服务,使用处理程序来发送消息和套接字通信。

I've been working with Android for well over a year now, but I still have trouble determining when different types of messaging/communication between processes/threads should be used. I'm mainly talking about broadcasting Intents, using AIDL for services, using Handlers to send messages and socket communication.

许多这样的工具可以用来完成相似的任务,但它更适合于特定的情况呢?

Many of these tools can be used to accomplish similar tasks, but which is better suited to particular situations?

推荐答案

这是一个pretty的开放式的问题,但让我采取射击在描述我所看到的画面内/间应用程序通信的工作最好的。

This is a pretty open ended question, but let me take a shot at describing how I see the intra/inter application communication working best.

一个Android的消息的一个关键方面是所有应用程序组件是松散结合的概念。因为所有应用程序都在一个单独的进程中运行,以及一个应用程序实际上可能包括若干应用程序(负责提供不同的活动或服务),该消息传递技术都是基于周围跨进程边界编组消息的想法。

One of the key aspects of Android messaging is the concept of all application components being loosely bound. Because all applications run in a separate process, and one 'app' may actually consist of several applications (responsible for providing different Activities or Services), the messaging techniques are all based around the idea of marshaling messages across process boundaries.

意图

有关消息的preferred技术,总是想用一个Intent只要有可能。这是最原始的方式在Android的传输邮件。

The preferred technique for messaging, always try to use an Intent whenever possible. It is the most 'native' way to transfer messages within Android.

优点的

使用意图的消息保持应用程序组件的松散结合,让你无缝多个应用程序之间传送消息。意图被大量使用的核心系统内启动活动和服务,以及广播和接收系统事件。

Using Intents for messaging maintains the loose binding of application components, letting you transfer messages seamlessly between several applications. Intents are used heavily within the core system to start Activities and Services, and to broadcast and receive system events.

使用额外束可以包括键/值对的原语作为意图内的有效载荷数据来容易地从一个应用程序组件信息传递给另一个。 - 即使这些组件在不同的进程运行

Using extras Bundles you can include key/value pairs of primitives as payload data within Intents to easily pass information from one application component to another - even if those components are running in different processes.

缺点的

由于意图旨在进程之间走,额外负载只支持基本类型。如果你需要使用一个Intent发送对象则需要解构成原语的一端,在另一重建它。

Because Intents are designed to go between processes, the extras payload only supports primitive types. If you need to send an object using an Intent you'll need to deconstruct it into primitives at one end and reconstruct it at the other.

应用类

如果您只想在单个进程中运行一个应用程序进行通信,这是一个方便的解决方案。

If you only want to communicate within a single application running in a single process this is a handy solution.

优点的

通过扩展应用程序类(和执行它作为一个Singleton),你得到的会存在的,只要你的任何应用程序组件存在的对象,提供了一个集中的地方来存储和应用程序组件之间传输的复杂对象的数据。

By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.

缺点的

该技术限制了你的消息,以组件在单个应用程序。

This technique limits your messaging to components within a single application.

服务绑定,IPC和AIDL

绑定到服务,您可以访问它的方法和交换对象吧。 AIDL是定义如何序列化对象到OS原语,以便它可以跨进程边界进行整理,如果你绑定到该服务在一个单独的应用程序运行的方式。

Binding to a service lets you access its methods and exchange objects with it. AIDL is a way of defining how to serialize an object into OS primitives so that it can be marshalled across process boundaries if the Service you're binding to is running in a separate application.

优点的

当您绑定到一个服务,你可以访问它,就好像它是调用类中的对象。这意味着你可以执行的服务方法和与它交流丰富的对象。

When you bind to a Service you have access to it as though it was an object within the calling class. That means you can execute methods on the Service and exchange rich objects with it.

请注意,如果你在不同的应用程序绑定到服务,你需要创建一个告诉机器人如何seralize /反序列化,你想要的应用程序之间传递的任何对象的AIDL定义。

Note that if you're binding to a Service in a different application process you'll need to create the AIDL definitions that tell Android how to seralize / deserialize any objects you want to pass between applications.

缺点的

创建AIDL类IPC是一些额外的工作,并结合创建服务和活动之​​间的附加依赖,可以使它更难为内核,以清理资源时,其他应用程序正在被饿死。

Creating the AIDL classes for IPC is a bit of extra work, and binding creates additional dependencies between Services and Activities which can make it harder for the kernel to clean up resources when other applications are being starved.

Marshelling消息是昂贵的,但。所以,如果你不执行方法上的服务,使用绑定和IPC可能是矫枉过正 - 看看你是否可以使用意图实现同样的事情

Marshelling messages across process boundaries is expensive though. So if you're not executing methods on a Service, using binding and IPC is probably overkill - see if you can achieve the same thing using Intents.

插槽

如果你诉诸插座内或在单个设备上运行的应用程序之间进行通信,它要么是因为没有别的办法,或者你已经错过了一次机会的地方。如果你的消息让该设备,然后套接字是好,速度快,替代性。如果你住在设备没准意图或IPC将是一个更好的选择。

If you're resorting to sockets to communicate within or between applications running on a single device, it's either because there's no other way or you've missed a trick somewhere. If your messages are leaving the device then sockets are a good, fast, alternative. If you're staying on the device chances are Intents or IPC is going to be a better option.

 
精彩推荐
图片推荐