Android cross-process communication - IPC mechanism (1)

This article is excerpted from – Android development art exploration

Introduction

IPC is the abbreviation of Inter-Process Communication, which means inter-process communication or inter-process communication, which refers to the process of data exchange between two processes.

Process and thread
Thread: The smallest unit of CPU scheduling, while thread is a limited system resource.
Process: A unit of execution, referring to a program or an application on PCs and mobile devices. A process can contain multiple threads.

enable multi-process

If there are multiple processes in an application, you can specify android:process in AndroidManifest.xml for the four components. There are two ways to set them, as follows:

<service android:name=".BookManagerService" android:process=":remote"/>
<service android:name=".BookManagerService" android:process="com.example.remote"/>

The difference is: the former is a private process of the current application, and components of other applications cannot run in the same process as it; the latter is a global process, and other applications can run in the same process as it through ShareUID.

Note: The four components do not specify the android:process attribute, then it runs in the default process, the default process name is the package name

Multiprocessing Considerations

Since Android allocates an independent virtual machine for each application, or an independent virtual machine for each process, different virtual machines have different address spaces in memory allocation, which leads to different virtual machines in different virtual machines. Accessing the same class object results in multiple copies.
Generally speaking, the use of multi-process will cause the following problems:
(1) The static member and singleton mode completely fail;
(2) The thread synchronization mechanism completely fails;
(3) The reliability of SharedPreferences decreases;
(4) Application will be created multiple times.

cross-process communication

There are mainly the following types:
(1) Intent transfers data
(2) File sharing and SharedPreferences
(3) Binder-based Messenger and AIDL
(4) ContentProvider
(5) Socket

Basic Concepts – Serialization

Serialization is required when transferring data through Intents and Binders and when persisting objects to storage devices or network transfers.
Object serialization can be achieved by implementing Serializable provided by Java or the Parcelable interface that comes with Android.

Serializable and Parcelable use
Serializable: Generally, objects need to be persisted to storage devices or used when objects are serialized and transmitted over the network, and it is best to define a serialVersionUID constant to ensure normal deserialization.
Parcelable: Mainly used for memory serialization, which is generally the first serialization in Android.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764817&siteId=291194637