To know how to use multiple processes, first know the multi-process concept in Android. In general, an application is a process, and the name of the process is the application package name. We know that processes are the basic unit of system allocation of resources and schedules, so each process has its own separate resources and memory space, and other processes cannot arbitrarily access the memory and resources of other processes. So how do you get your app to have multiple processes? Very simply, when our four components are registered in the Androidmanifest file, there is a property of android:process: Here you can specify the process in which the component is located. The default is the app's main process. When the system starts this component, it is created (if it has not yet been created), and then the component is created before it is specified as another process. You can overload the OnCreate method of the application class, print out its process name, and you can see clearly. When setting the Android:process property, there is a place to note: If it is android:process= ":d Eamon" to: the first name, it means that this is an application private process, otherwise it is a global process. The process name of the private process is automatically prefixed with the package name before the colon, while the global process does not. Generally we have private processes and seldom use global processes.
The obvious benefit of using multiple processes is to share the memory pressure of the main process. As our application grows larger and more memory is used, the separate components are put into different processes, and it does not occupy the memory space of the main process. Of course there are other benefits, the people will find many applications in the Android background process is a number of processes, because they have to reside in the background, especially instant messaging or social applications, but now many processes have been used to rot. The typical usage is to start an invisible lightweight private process, send and receive messages in the background, or do some time-consuming things, or start the process on boot, and then do the listening. There is also to prevent the main process from being killed Daemon, the daemon and the main process to monitor each other, a party was killed to restart it. There should be other benefits, and there's not much to say here.
The downside is that it takes up a lot of space in the system, so the system memory can easily fill up and lead to Kaka. Consumes the user's power. The application architecture becomes complex because it handles communication between multiple processes. Here's another question.
1. Use implicit intent for simple interprocess communication
Definition <activity> description in the Androidmanifest.xml file: 1, a <activity> includes: 0 or more <intent-filter> tags, it is mainly as a matching standard, The success of the match can be determined by <action>, <category>, <data> three tags together. It can then be started by other processes, using intent to carry parameters, to achieve communication.
2. Through the service definition Aidl interface, with intent this message-based in-process or interprocess communication model, we can intent to open a service, can jump to another activity through the intent, Whether the service or activity above is in the current process or in another process, whether it is the current application or another application service or activity, through the message mechanism can communicate!
But one drawback of interprocess communication through the messaging mechanism is that if the interaction between our activity and service is not a simple activity to start a service operation, but rather to send some control requests at any time, It is important to ensure that the activity can be connected to the service at any time during service operation.
The Aidl interface method is suitable for communication between activity and service, and is a mechanism for remote and local service communication.
Aidl implementation steps and invoking services the methods are somewhat different
1. Turn the Iservice.java file into a aidl file
2. A stub class is automatically generated to implement IPC
3. The defined man-in-the-middle object directly inherits stubs
4. To ensure that the Aidl file for 2 applications is the same as the package name that requires the Aidl file
5. Get the man-in-the-middle object stub.asinterface (IBinder obj)
How to use multithreading in Android development