Android process details, android details

Source: Internet
Author: User

Android process details, android details

Android process details


When an application is started, if no other component of the application is running, the system starts a new Linux Process for the application, which has only one thread, this is the well-known main thread. By default, all components of an application run in a process and thread (main. From the printing information of Logcat, we can see that the Application column in The Logcat view prints the name value of the current Application process, normally, the package Value of manifes is printed, because the progress value is not set for our Application tag. If no value is set, by default, the package value is used as the name of the current application process. Of course, you can arrange for different components to run in different processes, and you can create another thread for any program. When we update the interface through asynchronous operations, if Handler is used, a new thread is often created. For example:

New Thread ()

{

Public void run ()

{

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

MHandler. sendEmptyMessage (0 );

}

}. Start ();

The following describes how to run different components of the same application in different processes:
By default, all components of the same application run in the same process, and most programs do not have to change this situation. If you want different components of the same application to run in different processes, you can also use the manifest file.

All the items in the Manifest file that support the android: process attributes (<activity>, <service>, <worker ER>, and <provider>) can specify a process, the component after this attribute is set will run in the process corresponding to this attribute. You can set this attribute so that each component runs in its own process or only some of the components share a process. You can also set android: process to make the components of different applications run in the same process. The <application> element also supports the android: process attribute, which is used to specify a default value for all components. Android may decide to shut down a process at some time. For example, when the memory is insufficient, the system will automatically shut down the background processes of some other applications to keep the running processes running.

Process lifecycle: the Android system tries its best to maintain the lifecycle of a process until memory space needs to be made available for new and more important processes. To determine which one to kill and which to stay, the system places the process to different importance levels based on the component and component Status running in the process. When system resources are required, the lower the importance level, the first elimination. Android importance levels are divided into five levels (from high to low): (1) Foreground Process (2) visible process (3) Service Process (4) background process (5) Empty Process.

Process Priority:

When the system memory is insufficient, the android system will choose to kill some unimportant processes based on the process priority. Process priorities from high to low are:

1. Foreground process:
(1) A process contains a foreground activity that is interacting with the user;
(2) The process contains the service bound to the foreground activity.
(3) The process contains the service that calls the startForeground () method.
(4) The process contains services that are 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, and the foreground processes are almost never killed. Only when the memory is low to ensure that all foreground processes run at the same time will they choose to kill a foreground process.

2. Visible process:
(1) A process contains an activity that is not in the foreground but is still visible (the onPause () method of the activity is called). A dialog box is displayed when the activity is run, at this time, although the activity is not a foreground activity, it is still visible.
(2) Processes contain services bound to visible activity

The visible 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 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: A blank process does not contain any active process. The system often kills the empty process without any impact. The only reason for null processes to exist is to cache startup data.

The theoretical part has been completed. Now we can use a Demo to better understand it.

First, take a look at the Manifest file of the test Demo. 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 =" 21 "/> <! -- Android: process = "newpackage. test "sets the process name of this Demo to newpackage. test, that is, all components of this Demo are running in this process by default --> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: process = "newpackage. test "android: label =" @ string/app_name "android: theme =" @ style/AppTheme "> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. actio N. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <! -- Android: process = "NewProgress." Run the NewProgress component in NewProgress. process. Note: The value format of android: process must be: xxx. xxxx. This must contain a "."; otherwise, it cannot be compiled. The compiling environment is 5.0 --> <activity android: name = ". newProgress "android: label =" @ string/app_name "android: process =" NewProgress. "/> <activity android: name = ". nowProgress "android: label =" @ string/app_name "/> </application> </manifest>

As you can see from the code above, this Demo runs in the newpackage process. in test, the NewProgress Activity runs in NewProgress. in this process, let's take a look at the operation, as shown in:


The following code gets the name of the current process:

Package com. gc. testprogressdemo. uitls; import android. app. activityManager; import android. content. context;/*** return the name of the current process * @ author Android General **/public class GetProgressName {public static String getCurrentProgressName (Context context) {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 ;}}

This Demo is relatively simple, so we will not elaborate on it too much. The Demo will be attached at the end of the article.

For how to determine the level of the current process, you can modify the code returned by the above function

Return appProcess. processName + "level" + appProcess. importance;

The importance attribute reflects the process priority. For details, see the following table:


In actual development, if you want to obtain the progress status, you can view the importance attribute of the process you want to know.

In this Demo, I re-wrote the MainActivity and NewProgress Life Cycle methods. The log of Logcat printing is as follows:

12-10 13:05:13. 083: I/System. out (1452): MainActivity process onCreate: newpackage. test level 100 foreground Process
12-10 13:05:13. 091: I/System. out (1452): MainActivity process onStart: newpackage. test level 100 foreground Process
12-10 13:05:13. 095: I/System. out (1452): The process onResume where MainActivity is located: newpackage. test level 100 foreground Process
12-10 13:06:30. 335: I/System. out (1452): MainActivity process onPause: newpackage. test level 100 foreground Process
12-10 13:06:30. 595: I/System. out (1471): Process onCreate: NewProgress. Level 100 foreground Process
12-10 13:06:30. 595: I/System. out (1471): The onStart OF THE NewProgress process: NewProgress. Level 100 foreground Process
12-10 13:06:30. 599: I/System. out (1471): onResume OF THE NewProgress process: NewProgress. Level 100 foreground Process
12-10 13:06:30. 923: I/System. out (1452): MainActivity process onStop: newpackage. test level 130 visible process (classic scenario: Activity pop-up dialog box)
12-10 13:07:46. 199: I/System. out (1471): Process onPause: NewProgress. Level 100 foreground Process
12-10 13:07:46. 207: I/System. out (1452): MainActivity process onRestart: newpackage. test level 100 foreground Process
12-10 13:07:46. 211: I/System. out (1452): MainActivity process onStart: newpackage. test level 100 foreground Process
12-10 13:07:46. 211: I/System. out (1452): MainActivity process onResume: newpackage. test level 100 foreground Process
12-10 13:07:46. 647: I/System. out (1471): Process onStop: NewProgress. Level 400 background process
12-10 13:07:46. 647: I/System. out (1471): onDestroy: NewProgress. Level 400 background process



There are so many things to talk about. If you have any questions that you don't understand, you can reply to your questions. If there are any mistakes, please point them out.

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

Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/41851139


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.