Android multi-process series
- Binder for Android multiprocess
- Android multiprocess manual Binder class
- Android multiprocess Binder unbinding listener issues
- Accidental death and permissions verification for Android Multiprocess Binder
- Android Multiprocess Messenger
A method to enable multiple processes
There is only one way to use multiple processes on Android, and that is to specify the Android: Process property in AndroidMenifest for the four major components (Activity, Service, Receiver, and ContentProvider)
<service
android:name="com.xxq2dream.service.BookManagerService"
android:process=":remote" />
Copy the code
- With this setup, BookManagerService runs in a separate process named: package name :remote, for example, com.xxq2dream.android_ipc:remote
- The remote process is the private process of the current application, and components of other applications cannot run in the same process with it
- There is another way to set the android: Process attribute com.xxq2dream.android_ipc.remote. The process in this way is a global process, and other applications can run in the same process with it through ShareUID.
- The first is the one we use most often in our applications, which is the application’s private process
Problems after enabling multi-process mode
Android assigns a separate VIRTUAL machine to each process, and different virtual machines have different address Spaces for memory allocation, resulting in multiple copies of objects accessing the same class in different virtual machines. Multiple processes between one application can be interpreted as equivalent to two different applications using the SharedUID pattern.
- All four components running in different processes fail to share data in memory between them
- Static members and singletons are completely disabled (memory areas are different for different processes)
- Thread synchronization completely fails (different processes lock different objects)
- Application will be created multiple times
- The reliability of SharedPreferce deteriorates. (SharedPreferce is implemented by reading and writing XML files, and the system has a cache policy for reading and writing it. There is a SharedPreferce file cache in memory, so the concurrent write operations of multiple processes may cause data loss.)
The realization of multi – process communication
Use the Bundle
- Three of the four major components (Activity, Service, and Receiver) support Bundle data passing in intEnts
- All data in the Bundle needs to be serializable except for the basic types
- This is used to start another process from one process, such as an Activity, Service, or Receiver in one process, and transfer data at the same time
Using File Sharing
- Two processes exchange data by reading and writing to the same file
- Writing data to a file, of course, requires that the data be serialized and deserialized
- File sharing has no requirement on file format, as long as the data format is agreed upon by the read and write parties
- File sharing has concurrent read and write problems. Therefore, it is suitable for inter-process communication that does not require high data synchronization and should properly handle concurrent read and write problems
Use of messenger
- Messager is used to pass messages. The only fields that can be used in a Message are WHAT, arg1, arg2, Bundle, and replyTo. Custom Parcelable objects cannot be transmitted through object fields
- The Bundle in Message supports multiple data types, and the replyTo field is used to transport Messager objects for communication between processes
- Messager processes messages from clients in a serial manner and is not suitable for large numbers of concurrent requests
- The Messager method can only pass messages and cannot call methods across processes
Use AIDL
- The AIDL interface can be used by writing AIDL files and then generating corresponding Binder classes by the system, with which we can communicate across processes
- Only the following types are supported in AIDL files:
- Basic types, such as int, long, etc
- String and CharSequence
- List: Only ArrayList is supported, and every element in it must be supported by AIDL
- Map: Supports only HashMap, in which keys and values must be supported by AIDL
- AIDL: All AIDL interfaces themselves can also be used in AIDL files
- Custom Parcelable objects used in AIDL files and AIDL objects must be imported to display
- Out of basic data types, other types of parameters must indicate whether they are input or output parameters. In represents input parameters, out represents output parameters, and inout represents input and output parameters
Serializable interface and Parcelable interface
- Both Serializable and Parcelable interfaces can implement serialization, but there are differences
The Serializable interface
- Serializable interface implementation is relatively simple, only need to implement Serializable interface in the class to implement serialization
- SerialVersionUID can be specified or not specified, otherwise the system will generate it for us by default
- The purpose of serialVersionUID is to identify the current version of the class so that you can determine if the class has changed during deserialization. If the serialVersionUID is inconsistent, deserialization will fail
- If the serialVersionUID is manually specified, the deserialization will still succeed unless there is a destructive change to the class, such as adding a field after a version upgrade. If not, deserialization will fail
- Whether the serialVersionUID needs to be specified depends on specific requirements
- Static member variables and variables marked with transient keywords do not participate in the serialization process
Parcelable interface
- The serialization of Parcelable interface is slightly complicated, which requires the implementation of writeToParcel method, describeContents method and generator Creator
- The describeContents method returns 0, or 1 if the file descriptor is present in the object
- A Parcel in the Parcelable interface internally wraps serializable data that can be transferred freely in the Binder
Parcelable and Serializable interfaces
- Serializable interface is a Serialization interface in Java. It is easy to use but expensive. Serialization and deserialization require a large amount of I/O operations
- The Parcelable interface is the Android serialization method. It is a bit cumbersome to use, but it is very efficient. It is the recommended serialization method in Android, mainly for memory serialization
Welcome to follow my wechat official number, and learn and grow together with me!Copy the code