One of the three core features of the Android kernel AMS internal principle

Source: Internet
Author: User

The above class is the full name of AMS, and the other two core functions are Windowmanagerservice.java and View.java

The main functions provided by AMS:

Unified scheduling for each application memory management process Management

Several important data classes are defined in AMS to hold processes, activities, and tasks (Task)

Processrecord.java information about the process that is logged

The internal variables in this class can be divided into three parts, namely process file information, process memory state information and activity, service, etc. contained in the process.

The class is under the framework/base/services/java/com/android/server/am/path, and the last am on the path represents activity Manager, and the important classes related to AMS are in that directory.

Activityrecord.java

The Activityrecord data class is used in AMS to hold information about each activity. The history contains an int task variable that holds which task the activity belongs to. You can use the Intent.flag_new_task identity to tell AMs to recreate a task for the activated activity.

Taskrecord.java

The concept of using tasks in AMS ensures the order in which activity starts and exits

Important scheduling related variables in AMS
// How long we wait for until we timeout on key dispatching. button unresponsive timeout time, which is the standard of Google, the domestic Lenovo phone most of the design according to this standard, Huawei mobile phone is not according to this standard Static int 5*;
// Maximum Number of recent tasks that we can remember. Static int  - ; The maximum number of activity that the AMS records, more than 20, discards the oldest recorded activitystaticint broadcast_timeout= * 1000 Broadcast Timeout time
Final arraylist<pendingactivitylaunch> mpendingactivitylaunches            new arraylist< Pendingactivitylaunch> (); Temporarily save client requests
int pause_timeout=   -
AMS notifies the application that the process is suspending the specified activity,
The endurance of AMS is limited, only 500 milliseconds.
If the application process still has a stop in that constant time,
AMS is forcing a pause to close the activity.
That's why application design cannot do too much things in OnPause ().

Activitystack is the stack that stores activity information.

Related variables:

Final ArrayList mhistory=new ArrayList (); All running activity is saved.

Private final ArrayList mlruactivities=new ArrayList ();

LRU (Latest recent used), saving all activity used in the past

Unified scheduling of each application activity

The activity does not correspond to an application, Activitythread only one application, so Android allows multiple applications to run at the same time, actually allowing multiple activitythread to run simultaneously.

In Android, the basic idea of activity scheduling is this: Each application process to start a new activity or stop the current activity, must first report to AMS, and not "unauthorized processing." AMS records all application processes internally, and when AMs receives a start or stop report, it first updates the internal records before notifying the corresponding client process to run or stop the specified activity. Because the AMS has all the activity records, it is natural to be able to dispatch these activity, and automatically kill the background activity according to the activity and the state of the system memory.

Self-summary: Activity start or stop notification Ams,ams decide whether to perform the action, the amount of activity >20, the earliest activity will be the AMS to forget, new added in the activity into the stack

StartActivity at the bottom of the work map

For example, click A-B:
When you click A, when AMS receives a customer request startactivity (), the current activity is paused first, so it is determined if the mresumedactivity is empty. In general, this value is not NULL, and if the AMS is not empty, the client is notified to pause, and the activity is executed and returned.
When the a process completes the pause and reports AMS, the AMS starts executing completepaused (), which first checks if the target activity is in the Mhistory list, and if the target process is still running, the target activity is only in the stop state , there is no finish, so notify the B process directly resume the specified activity can be.

Activity four startup mode analysis

  

Android:launchmode settings are in the Androidmanifest.xml file
Standard (default), Singletop,singletask,singleinstance
<activity android:name="Com.example.android_launchmodel. Activity1"Android:launchmode=" Standard"/>default<activity android:name="Com.example.android_launchmodel. Activity2"Android:launchmode="Singletask"/><activity android:name="Com.example.android_launchmodel. Activity3"Android:launchmode="Singletop"/><activity android:name="Com.example.android_launchmodel. Activity4"Android:launchmode="singleinstance"/>
Standard

This is the default mode, and an activity instance is created each time the activity is activated and placed in the task stack

Singletop

If there is an instance of the activity at the top of the task, the instance is reused (Onnewintent () of the instance is invoked), otherwise a new instance is created and placed on top of the stack, even if an instance of the activity already exists in the stack, and a new instance is created as long as it is not at the top of the stack.

Singletask

If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so instances above it will be moved out of the stack. If the instance does not exist in the stack, a new instance is created and placed in the stack

To add an example, build 4 activity classes (such as the Androidmanifest.xml file Activity2 for Singletask mode) in Oncreate,onstart,onPause, Onstop,ondestroy method Call Log

Activity Boot Sequence is

Activity1àactivity2àactivity3àactivity4àactivity2

When the activity1 into the stack-"activity2 into the stack-" activity3 into the stack-"activity4 into the stack-(in Activity4 because the Activity2 is Activity2 mode, Will find a Activity2 instance in the task stack. The next step is to put the Activity2 instance on top of the stack, but there are instances of Activity3 and 4 on the Activity2 instance. They can only get out of the stack. )

--"The OnDestroy () method of the Activity3 and Activity4 is seen to be executed.

When Activity4 starts Activity2, Activity2 's OnCreate () is not executed (this also indicates that AMS did not recreate the Activity2 instance into the task stack, The OnCreate () method is called only when the activity is first created)

The Activity3 and Activity4 in the stack are already out of the stack, and the Activity2 instance in the stack is started.

Press back two times and return to the desktop.

There are no instances of activity1 and Activity2 in the stack (other similar)

singleinstance

Create an instance of the activity in a new stack and have multiple apps share that activity instance in the stack. Once the activity instance of the pattern already exists in a stack, any application that activates the activity will reuse the instance in that stack (the onnewintent () of the instance is invoked). The effect is that multiple apps share an app, regardless of who activates the Activity into the same app. In this mode, no new acitvity instances are pressed into the stack, other places are the same as ' Singletask '. This means that there is only one instance of activity in the stack. This is a very special pattern that can only be used in a program that has only one activity.

For reference, the following other examples: http://blog.csdn.net/liuhe688/article/details/6754323

AMS Memory Recovery Rules

1, the foreground process, refers to those with the user operation of the relevant process. Details include:

    • Activity that is interacting with the user
    • Contains a service that is serving the activity that interacts with the user.
    • Contains a service that is executing the oncreate (), or OnStart (), Ondestory () method.
    • Contains a broadcastreceiver that is executing the onreceive () function.

2, the visual process, although no interaction with the user, but can affect the user to see the content.

    • A dialog box that is located on the activity
    • A service

3. Service process: All services that are started by using StartService () are called service processes.

4. Empty process: The process does not contain any componet, including the Activity,service,receiver object, because the reason for preserving these processes is to reduce the overhead required for re-creation.

The code meaning of the activity life cycle

In the past application development process, most people have been aware of the main states of activity life and know how to do different things in these states. But there may be some doubts, such as where the real difference between the OnStart () method and the OnStop method is. Although you know that OnStop represents the cessation of activity, OnStart represents the beginning of activity. The question is, where is the difference between start and stop?



One of the three core features of the Android kernel AMS internal principle

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.