Android process priority

Source: Internet
Author: User

Android process priority

Processes in android

By default, all components in the same application run in the same linux Process. when you start A component A, if there is already A component B in the running state and A and B belong to the same application, component A will run in the process where component B is located. otherwise, A new linux Process will be created for.

The developer can also specify different running processes for component in the application. manifest. xml file, , , All labels support the android: process attribute. You can use this attribute to specify the running process for component. the tag also supports setting the android: process attribute to specify the default running process for all components under the application.

 

Process Priority

When the system memory is insufficient, the android system will choose to kill some less important processes based on the process priority. The process priority ranges from high to low:

1. Foreground process. The following processes are foreground processes:

A. The process contains a foreground activity that is interacting with the user;

B. The process contains the service bound to the foreground activity;

C. The process contains the service that calls the startForeground () method;

D. The process contains services that are executing the onCreate (), onStart (), or onDestroy () method;

E. The process contains the BroadcastReceiver that is executing the onReceive () method.

The number of foreground processes in the system is very small, and the foreground processes are almost never killed. Only when the memory is low to ensure that all foreground processes run simultaneously will they choose to kill a foreground process.

2. Visible process. The following processes are visible processes:

A. the process contains the activity that is not in the foreground but still visible (the onPause () method of the activity is called, but the onStop () method is not called ). A dialog box is displayed when you run the activity. Although the activity is not a foreground activity, it is still visible.

B. The process contains the service bound to the visible activity.

The visible process will not be killed by the system, unless it is necessary to ensure the running of the foreground process.

3. service process. The process contains the started service.

4. background process. the process contains an invisible activity (the activity after the onStop () method is called ). the background process does not directly affect the user experience. To ensure that the foreground process/visible process/service process runs, the system may kill a background process at any time. an activity that correctly implements the lifecycle method is killed by the system when it is in the background. It can be restored when the user restarts it.

5. empty Process. any active process is an empty process. the system often kills empty processes without any impact. the only reason for the existence of a null process is to cache startup data so that it can be started faster next time.

 

Additional description of Process Priority

1. The system will give the process the highest priority possible. For example, if a process contains both the started service and the foreground activity, the process will be considered as the foreground process.

2. because of the dependency between components, the priority of processes may be increased. if process A serves process B, the priority of process A cannot be lower than process B. for example, if the ContentProvider component of process A is serving A component of process B, or the service component of process A is bound to A component of process B, process A has A higher priority than process B (if the priority of process A is indeed lower than process B according to the priority rules, the system will choose to increase the priority of process A to be the same as process B ).

3. because the service process has a higher priority than the background process, if the activity needs to perform time-consuming operations, it is best to start a service to complete. of course, the time-consuming operations can be performed by the promoter thread in the activity, but the disadvantage of this operation is that once the activity is no longer visible, the process of the activity becomes the background process, when the memory is insufficient, background processes may be killed by the system at any time (however, the time-consuming operations after the service is started may cause data interaction problems. For example, if time-consuming operations require real-time updating of the UI control status, service is not a good choice ). for the same reason, you should not perform time-consuming operations in BroadcastReceiver, but start the service to complete it. (Of course, the life cycle of BroadcastReceiver is too short, it also determines that time-consuming operations cannot be performed in it ).

 

Threads in android

The system does not start a new thread for every component in the process, and all components in the process are instantiated in the UI thread. For more information about the multithreading mechanism in android, see my another blog http://www.bkjia.com/kf/201205/132323.html

Always remember:

1. Do not block the UI thread. If the UI thread is blocked or time-consuming, the UI thread cannot respond to user requests.

2. You cannot update the UI in a non-UI thread (also known as a working thread) Because android UI controls are thread-insecure.

As described above, developers often start working threads to complete time-consuming or blocking operations. If you need to update the UI status during the execution of the working thread, you should notify the UI thread to do so.

 

Inter-thread Communication

See the following code:

Java code
Public void onClick (View v ){
New Thread (new Runnable (){
Public void run (){
Bitmap B = loadImageFromNetwork ("http://up.2cto.com/2012/0519/20120519094618735.png ");
MImageView. setImageBitmap (B );
}
}). Start ();
}
The above code is incorrect. mImageView. setImageBitmap (B) violates the second criterion-the UI cannot be updated in the work thread.

Inter-thread communication can solve the problem of how the worker thread notifies the UI thread to update the control. android provides three inter-thread communication solutions:

1. Call the following methods:

Activity. runOnUiThread (Runnable)

View. post (Runnable)

View. postDelayed (Runnable, long)

If the three methods are called in the working thread, the Runnable parameter encapsulation operation in the method will be executed in the UI thread.

This method can be used to correct errors in the example:

Java code
Public void onClick (View v ){
New Thread (new Runnable (){
Public void run (){
Final Bitmap bitmap = loadImageFromNetwork ("http://up.2cto.com/2012/0519/20120519094618735.png ");
MImageView. post (new Runnable (){
// The run method will be executed in the UI thread
Public void run (){
MImageView. setImageBitmap (bitmap );
}
});
}
}). Start ();
}
2. handler mechanism. the Handler mechanism allows developers to call the sendMessage () method of the handler object bound to the UI thread in the working thread to send a message to the Message Queue of the UI thread, the UI thread extracts messages from the message queue and completes processing as appropriate.

3. use AsyncTask class. create a subclass of the AsyncTask class and overwrite the onPreExecute (), doInBackground (), onProgressUpdate (), onPostExecute () methods as needed. for more information about how to use the AsyncTask class, see the document:

A. the AsyncTask class is a generic class with three generic parameters. the first parameter specifies the parameter type of the execute method, the second parameter specifies the parameter type of the onProgressUpdate () method, and the third parameter specifies the return value type of the doInBackground () method and onPostExecute () the parameter type of the method.

B. execution Process: Call the execute method of the AsyncTask class in the UI thread (only this step is controlled by the programmer) --> the system calls onPreExecute (), this method is executed in the UI thread --> the system calls the doInBackground () method, which is executed in the work thread --> the publishProgress () method is called every time in the doInBackground () method, after the onProgressUpdate () method --> doInBackground () method is executed in the UI thread, the system will call the onPostExecute () method and () the Return Value of the method is passed to the form parameter of the onPostExecute () method. the onPostExecute () method is executed in the UI thread.

This method can also be used to correct errors in the example:

Java code
Public void onClick (View v ){
New downloadimagetask(cmd.exe cute ("http://up.2cto.com/2012/0519/20120519094618735.png ");
}
Private class DownloadImageTask extends AsyncTask {
Protected Bitmap doInBackground (String... urls ){
Return loadImageFromNetwork (urls [0]);
}

Protected void onPostExecute (Bitmap result ){
MImageView. setImageBitmap (result );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.