1. What is multi-process?Normally, in the Android system, after an app is started, it will only run in one process, whose process name is the package name of apk, and all components will run in this process; However, if you need to run certain components (such as Service, Activity, etc.) in a separate process, you need to use the android:process attribute. We can set the android:process attribute for the Android component to make it run in the specified process. 1. Advantages of multi-process:①Use more memory The Android system has a limit on the memory usage of each application process, and the larger the memory usage of a process, the more likely it is to be killed by the system. Running a component in a separate process can reduce the memory used by the main process, avoid OOM problems, and reduce the probability of being killed by the system. ②Realize multiple modules For example, WeChat’s mini-programs (one main process and one mini-program process) and Alipay’s mini-programs; When you start a mini program, the mini program process will be started, which will not occupy the memory of the main process, making the mini program process separate and much faster. ③The child process crashes, and the main process can continue to work ④The main process exits and the child process can continue to work ⑤Implementing the daemon process Use C/C++ through JNI to call the fork() method to generate a child process. Generally, developers will use this method to create some daemon processes to achieve anti-killing and keep-alive effects. 2. Problems caused by multiple processes①Static members and singleton mode are invalid ②Thread synchronization mechanism fails ③SharedPreferences reliability is reduced ④Application is created multiple times ⑤Multi-process interaction is troublesome Causes: In Android, the system allocates an independent virtual machine for each application or process. Different virtual machines naturally occupy different memory address spaces. Therefore, objects of the same class will produce different copies, resulting in failure of shared data and inevitable failure to achieve thread synchronization. 2. Multi-process communication modeThere are mainly the following multi-process communication methods supported in Android. Each of them has its own advantages and disadvantages. You can choose according to the usage scenario:
Broadcasting is a passive cross-process communication method. When a program sends a broadcast to the system, other applications can only passively receive the broadcast data. This is just like a radio station broadcasting. Listeners can only listen passively but cannot actively communicate with the radio station. It is relatively simple to send broadcasts in the application. Just call the sendBroadcast method. This method requires an Intent object. The Intent object can be used to send the data to be broadcasted.
This time we will introduce AIDL and Messenger, two commonly used interaction methods in daily APP development. As shown 1. Multi-process AIDL communicationAIDL is the Android interface definition language, which is a description language used to define the communication interface between the server and the client. It can be used to define the programming interface of the inter-process communication (IPC) between the client and the server. In Android, processes cannot share memory (user space), and communication between different processes is generally handled using AIDL. Simple steps to use: Create an AIDL file (with the extension .aidl); Expose an interface to the client (by establishing a Service and returning an instance of the Stub class in the onBind() method); The server implements the Java interface generated by the AIDL file (the system will automatically generate the corresponding Java interface); The client connection is bound to the remote service. ①Create AIDL file Create an AIDL file, declare the interface that the server wants to expose to the client, then create a Service to listen to the client's connection request and implement the interface in the AIDL file Note that for the convenience of development, we generally put AIDL related files in the same package, so that when the client is another application, the entire package can be easily copied to the client project. First, let's take a look at the data types supported by AIDL files:
Book is a class that implements Parcelable. It only defines the name field. According to the regulations, if the AIDL file uses a custom Parcelable object, a Book.aidl file must also be provided.
②ILibraryManager.aidl defines the interface that the server will expose to the client:
③The next step is the Java interface generated by the AIDL file. Before writing the service class, you must compile the project first so that the java class generated by AIDL can be used in the service class. First, create an mBinder object through ILibraryManager.Stub(), implement the interface method defined in ILibraryManager.aidl, return the created mBinder in the onBind() method, and add data in the service onCreate(). Finally, register the service in AndroidManifest.xml:
④The client connects and binds the remote service First implement the ServiceConnection interface, and convert the IBinder object into an ILibraryManager object in the onServiceConnected() method. Through this object, you can call the methods declared in ILibraryManager.aidl. Next, bind the service:
2. Notification function of aidlFirst, create an IOnNewBookArrivedListener.aidl interface for the server to notify the client:
The server must register before it can receive notifications, and it can also unregister, so we need to add two methods to the previous ILibraryManager.aidl:
Modify ManagerService:
The RemoteCallbackList class is used here. It is a class provided by the system specifically for deleting cross-process listeners. It is difficult to ensure that the listener registered by the client and the listener stored by the server are the same using ordinary collections, which will cause the registration to fail. Client code modification
3. Messenger communicationMessenger is a lightweight multi-process communication method. It is encapsulated based on AIDL and can be regarded as a simplified version of AIDL. It supports one-to-many serial real-time communication. Only one request is processed at a time, and there is no concurrency problem. Similar to the use of AIDL, but much simpler, and also requires the implementation of the server and client. First, let's look at the server
See the client implementation
In onServiceConnected(), convert the server-side Binder into a server-side Messenger object, and then send a message. Since the server needs to reply to the client, a Messenger object needs to be created on the client side and attached to the message and sent to the server for use. Multi-process summary:1. Multi-process apps can apply for multiple copies of memory in the system, but they should be used reasonably. It is recommended to put some high-consumption but infrequently used modules into independent processes, and unused processes can be manually closed in time; 2. Multiple processes occupy more memory and increase power consumption, so please pay attention to this; 3. When each process is created, the Application's onCreate will be executed for initialization. If there is no processing for different processes at this time, the onCreate initialization business will be executed multiple times, which is unnecessary and multiple initializations are prone to cause problems, so the corresponding business needs to be initialized according to different processes. This article is reprinted from the WeChat public account "Android Development Programming". You can follow it through the following QR code. To reprint this article, please contact the Android Development Programming public account. |
>>: What is the value of B-side designers? Let’s take a look at the director-level analysis (Part 1)
Google I/O 2015 will be held at Moscone Center We...
How much is the quote for customized nutritional ...
Download the report: Add 199IT WeChat official ac...
How to promote a new App? Nowadays, App promotion...
Online content operations can be understood as th...
As everyone knows, ASO refers to the search optim...
Recently, it was reported that Ted Livingston, th...
In life, we are bound to get hurt sometimes. Expe...
When we go to the doctor or buy medicine, we ofte...
In marketing psychology, herd mentality, greed fo...
[[246116]] A large-scale theft of Apple IDs among...
Review expert: Dong Wenpan, associate professor o...
Tesla’s Battery Day had just ended when tragedy s...
It is said that "after Laba Festival it is t...
Background: It has been four years since Changba ...