Introduction
When the application's components run for the first time, Android launches a Linux process with only one thread of execution. By default, all components of the application run in this process and thread. However, you can schedule components to run in other processes, and you can derive other threads from the process. This article describes the processes and threads of Android from the following points:
- 1. Process
- 2. Threads
- 2.1. Remote procedure Call (Remotes procedure Calls,rpcs)
- 2.2. Thread-Safe methods
1. Process
The process in which the component runs is controlled by the manifest file. Component elements--<activity>,<service>,<receiver>,<provider>, Has a process property that specifies which processes the component is running in. This property can be set for each component to run in its own process, or some components to share a process while others are not shared. They can also set up components for different applications to run in the same process-assuming that the applications share the same Linux User ID and are assigned the same permissions. The <application> element also has the process attribute, which sets a default value for all components.
All components are instantiated in the main thread of a particular process, and the system invocation component is dispatched by the main thread. Separate threads are not created for each instance, so methods that correspond to these calls-such as the View.onkeydown () report with the user's behavior and lifecycle notifications-always run in the main thread of the process. This means that no component should perform long or blocking operations (such as network operations or cyclic computations) when called by the system, as this will block other components in the process. You can derive a separate thread for long operations.
public boolean onKeyDown(int keycode,keyevent event): The default implementation is KeyEvent.Callback.onKeyMultiple () when the keycode_ of the view is pressed Dpad_center or Keycode_enter is then released when executed, if the view is available and clickable.
Parameters
KeyCode-Indicates that the button was pressed by the key code, from KeyEvent
Event-A KeyEvent object that defines a button action
return value
Returns TRUE if you handle the event, or False if you want the next recipient to handle the event.
When the memory remains small and other processes request large memory and needs to be allocated immediately, Android will recycle some processes and the application components in the process will be destroyed. When they run again, a process starts again.
When deciding which process to end, Android weighs their relative weights on the user's importance. For example, it is easier to close a process than to run an active process that is visible on the screen (foreground process), and its activity on the screen is not visible (background process). Determines whether the process is terminated, depending on the state of the component running in the process. The state of the component will be described in the following-the component life cycle .
2. Threads
While you may be restricting your application to a process, sometimes you will need to derive a thread to do some background work. Because the user interface must respond quickly to the user's actions, the active hosted thread should not do some time-consuming operations such as network downloads. Any operations that cannot be completed in a short time should be assigned to another thread.
Threads are created in code with standard Java threading objects, and Android provides some handy classes to manage threads-Looper is used to run message loops in the thread,Handler users to process messages, Handlerthread A thread that the user sets a message loop.
Looper class
This class of users runs the message loop in a thread. The thread does not have a message loop by default, and you can call prepare () in threads to create a run loop, and then call Loop () to process the message until the loop ends. Most of the message loop interactions are through the handler class. The following is a typical example of executing a looper thread, using prepare () and loop () to create an initial handler interaction with Looper:
Class Looperthread extends Thread {
Public Handler Mhandler;
public void Run () {
Looper.prepare ();
Mhandler = new Handler () {
public void Handlemessage (Message msg) {
Process incoming messages here
}
};
Looper.loop ();
}
}
For more information about Looper and Handler,handlerthread Please refer to the relevant information.
2.1. Remote Procedure Call (Remotes procedure Calls,rpcs)
Android has a lightweight remote procedure call mechanism-the method is executed locally but in a remote (another process) and the result is returned to the caller. This requires decomposing the method call and its accompanying data into a hierarchy that the operating system understands, transferring from the local process and address space to the remote process and address space, and reassembling the call. The return value is transmitted in the opposite direction. Android provides all the code to do these things, so we can focus on defining and executing the RPC interface itself.
An RPC interface contains only methods. All methods are executed synchronously (local method blocking until remote method execution completes), even if there is no return value. In short, the mechanism works as follows: First, you declare an RPC interface that you want to implement with a simple IDL (interface Definition language, Interface Definition language). From this declaration, theaidl tool generates a Java interface definition that is provided to both local and remote processes. It contains two inner classes, as shown in:
The inner class has all the code needed to manage the remote procedure call for the interface that you define with IDL. Both of these internal classes implement the IBinder interface. One of these is that it is used internally by the system, and you can write code that ignores it. The other one is the Stub, which extends from the Binder class. In addition to the internal code used to effectively invoke the IPC (interprocess communication), the inner class also contains the method declaration in the RPC interface declaration. You can define Stub subclasses to implement these methods, as shown in.
Typically, a remote procedure has a service management (because the service can inform the system about the process and the information about other processes it connects to). It has interface files generated by the aidl tool and RPC methods implemented by the Stub subclasses. The client for the service has only the interface files generated by the aidl tool.
Here's how the service establishes a connection to its clients:
- The client (on the local side) of the service should implement the onserviceconnected () and onservicedisconnected () methods, so it is notified when the connection to the remote service is successful and the connection is disconnected. Then call Bindservice () to establish the connection.
- The Onbind () method of the service will be implemented to accept or reject the connection, depending on the intention it receives (the intent is passed to Binservive ()). If the connection is accepted, it returns an instance of a Stub subclass.
- If the service accepts a connection, Android calls the client's onserviceconnected () method and passes it to a IBinder object, returning a proxy for the Stub subclass that is managed by the service. Through the proxy, the client can invoke the remote service.
Here is simply a description that omits some of the details of the RPC mechanism. You can check the relevant information or continue to follow the Android development journey, which will be served for you.
2.2. Thread-Safe Methods
In some cases, the method you implement may be called by more than one thread, so it must be written as thread-safe. This is correct for the remote invocation method-as the RPC mechanism discussed in the previous section. When a method implemented in a IBinder object is called from the ibinder process, this method executes in the caller's thread. However, when called from another process, the method chooses an execution in the thread pool in the ibinder process that is maintained by Android, which is not executed in the main course of the process. For example, a service's Onbind () method is called in the main thread of the service process, and the method that executes in the object returned by Onbind () (for example, the Stub subclass that implements the RPC method) is called in the thread pool. Because the service can have more than one client, you can have more than one thread executing the same IBinder method at the same time. Therefore, theIBinder method must be thread-safe.
Similarly, a content provider can accept data requests from other processes. Although the contentresolver and contentprovider classes Hide how the process communication is managed, the contentresolver method that corresponds to which request--query (), Insert (), delete (), update (), GetType (), are called in the thread pool of the content provider's process, not in the primary thread of the process. Because these methods can be called from any number of threads at the same time, they must also be implemented as thread-safe.
Go Android Development Tour: Processes and Threads