Android Process detailed

Source: Internet
Author: User

Android Process detailed


When an application starts, if no other components of the application are already running, then the system launches a new Linux process for the application, which has only one thread, the main thread that we know well. By default, all components of an app run in a process and thread (main). From Logcat's print message, it can be seen that the application column in the Logcat view prints the name value of the currently applied process, and typically prints the value of the Manifes package. This is because our application label does not set the value of progress, and if it is not set, the system defaults to the package value as the name of the currently applied process. Of course, you can completely schedule different components to run in different processes, and you can create additional threads for any program, which is when we do the asynchronous operation update interface, and if we use handler, we tend to create a new thread. Such as:

New Thread ()

{

public void Run ()

{

..................................................

Mhandler.sendemptymessage (0);

}

}.start ();

Here's how to implement different components of the same application running in different processes:
By default, all components of the same application run in the same process and most programs do not have to change the situation, and if you want to run different components of the same application in different processes, you can do so through the manifest file.

All those items in the manifest file that support the android:process attribute (<activity>,<service>,<receiver>, and <provider> ), you can specify a process in which the component after the property is set will run in the process that corresponds to that property. You can set this property so that each component runs on its own process or just some of its components share a process, and you can also set android:process so that components of different applications can run on the same process. The <application> element also supports the Android:process property, which is used to specify a default value for all components. Android may decide to close a process at some point, such as when there is not enough memory, the system automatically shuts down some other app's background process to keep the running process running.

The life cycle of a process: the Android system tries to maintain the life of a process until it eventually needs to free up memory for new and more important processes. To decide which one to kill, the system places the process at a different level of importance based on the state of the components and components that are running in-process. When system resources are needed, the lower the importance level is first eliminated. Android importance level is divided into 5 levels (high to Low): (1) foreground process (2) visible process (3) service process (4) Background process (5) empty process.

Priority of the process:

When the system is running low on memory, the Android system will kill some less important processes based on the process priority selection. The process priority is from high to low, respectively:

1, the foreground process, the following process for the foreground process:
(1) The process contains the activity that is interacting with the user in the foreground;
(2) The process contains service bound to the foreground activity
(3) The process contains a service that calls the Startforeground () method
(4) The process contains service that is executing the oncreate (), OnStart (), or OnDestroy () method
(5) The process contains the broadcastreceiver that is executing the onreceive () method

The number of foreground processes in the system is very small, the foreground process is almost never killed, and only when the memory is low enough to guarantee that all foreground processes are running concurrently will the process of killing a foreground be selected.

2. Visible process, the following process is visible process:
(1) The process contains activity that is not in the foreground but is still visible (called the activity's OnPause () method), typically when the activity is run with a popup dialog, while the activity is not the foreground activity, but its still visible.
(2) A service that is included in the process for visible activity bindings

The visual process will not be killed by the system unless it is necessary to ensure that the foreground process is running.

3. Service process: The process contains the service that was started

4. Background process: The process contains invisible activity (activity after the OnStop () method call). Background processes do not directly affect the user experience, and in order to keep the foreground process/visual process/service process running, it is possible for the system to kill a background process at any time. A correct implementation of the life cycle method of activity in the background is killed by the system, you can restart it when the user to restore the previous running state.

5, empty process: does not contain any active process is an empty process, the system often kills empty processes, this will not have any impact. The only reason an empty process exists is to cache some boot data.

Now that the theory is over, let's take a demo to deepen our understanding.

First look at the test demo manifest file, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/         Android "package=" Com.gc.testprogressdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/><!--android:process=" Newpackage.test "Set this The process name of the demo is Newpackage.test, that is, all components of this demo are running in the process by default--<application android:allowbackup= "true" Android: icon= "@drawable/ic_launcher" android:process= "Newpackage.test" android:label= "@string/app_name" Andro Id:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER  "/> </intent-filter> </activity> <!--Android:process= "Newprogress."        Run the Newprogress component in the newprogress. Process. Note: The value of android:process must be in the format: xxx.xxxx.        This must contain a ".", otherwise the compilation does not pass. Test the compilation environment to 5.0--<activity android:name= ".            Newprogress "android:label=" @string/app_name "android:process=" newprogress. " /> <activity android:name= ". Nowprogress "android:label=" @string/app_name "/> </application></manifes T>

As you can see from the code above, this demo is running in the process newpackage.test, newprogress the activity is running in newprogress. In this process, let's look at the run, as shown in:


Here's the code to get the name of the current process, as follows:

Package Com.gc.testprogressdemo.uitls;import Android.app.activitymanager;import android.content.context;/** * Returns the name of the current process * @author Android General * */public class Getprogressname {public static String Getcurrentprogressname (Context Contex t) {int pid=android.os.process.mypid (); Activitymanager mactivitymanager= (Activitymanager) Context.getsystemservice (Context.activity_service); for ( Activitymanager.runningappprocessinfo appProcess:mActivityManager.getRunningAppProcesses ()) {if (appprocess.pid== PID) {return appprocess.processname;}} return null;}}

Because this demo is relatively simple, it is not too much elaboration, the end of the article will be attached to the demo.

As for how to determine which level of the current process is a process, you can change the function return code above to

Return appprocess.processname+ "level" +appprocess.importance;

Where importance is the property of the reaction process priority, see the following table:


In real development, if you want to get the status of a process you can see the importance property of the process you want to know.

In this I have rewritten the life cycle method of mainactivity and Newprogress in the demo, and the Logcat print log is as follows:

12-10 13:05:13.083:i/system.out (1452): Mainactivity process OnCreate:newpackage.testLevel 100Foreground Process
12-10 13:05:13.091:i/system.out (1452): Mainactivity process OnStart:newpackage.testLevel 100Foreground Process
12-10 13:05:13.095:i/system.out (1452): Mainactivity process Onresume:newpackage.testLevel 100Foreground Process
12-10 13:06:30.335:i/system.out (1452): Mainactivity process OnPause:newpackage.testLevel 100Foreground Process
12-10 13:06:30.595:i/system.out (1471): Newprogress process OnCreate:newprogress.Level 100Foreground Process
12-10 13:06:30.595:i/system.out (1471): Newprogress process OnStart:newprogress.Level 100Foreground Process
12-10 13:06:30.599:i/system.out (1471): Newprogress process Onresume:newprogress.Level 100Foreground Process
12-10 13:06:30.923:i/system.out (1452): Mainactivity process OnStop:newpackage.testLevel 130visible Process (Classic Scene: Activity popup dialog box)
12-10 13:07:46.199:i/system.out (1471): Newprogress process OnPause:newprogress.Level 100Foreground Process
12-10 13:07:46.207:i/system.out (1452): Mainactivity process Onrestart:newpackage.testLevel 100Foreground Process
12-10 13:07:46.211:i/system.out (1452): Mainactivity process OnStart:newpackage.testLevel 100Foreground Process
12-10 13:07:46.211:i/system.out (1452): Mainactivity process Onresume:newpackage.testLevel 100Foreground Process
12-10 13:07:46.647:i/system.out (1471): Newprogress process OnStop:newprogress.Level 400Background Process
12-10 13:07:46.647:i/system.out (1471): Newprogress process OnDestroy:newprogress.Level 400Background Process



To talk about so much, if you have what do not understand can answer questions, if there is wrong, also please point out.

demo:http://download.csdn.net/detail/gc_gongchao/8245593

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/41851139


Android Process detailed

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.