Implementation method and example of Android 6.0 fixed screen function

Android fixed screen feature

Maybe you don't know what it is when you see this title. Let me explain first. After Android 6.0 is enabled in settings - > Security - > fixed screen, and then long press the home key to display several recent activities. You can select a pushpin button to enable the fixed screen function. After the screen is enabled, the screen can only be fixed to the activity switch on the set task.

1、 Set fixed screen

Let's first look at the code of systemui / SRC / COM / Android / systemui / presents / screenpinningrequest. Java. This code is the code that displays several activities by long pressing the home key, and then pressing the button of the pushpin. Here, the startlocktaskmodeoncurrent function of AMS is called directly.

Let's take a look at the startLockTaskModeOnCurrent function of AMS. First we call the topRunningActivityLocked of ActivityStackSupervisor to get the first Activity, then we call the startLockTaskModeLocked function, the parameter is TaskRecord.

Let's look at the toprunningactivitylocked function. First, get the top activity from mfocusedstack. If you don't traverse all mstacks to get.

The startlocktaskmodelocked function mainly calls the setlocktaskmodelocked function of activitystacksupervisor. Let's look at this function. Our task is not null. The first time mlocktaskmodetasks is empty, a lock will be sent_ TASK_ START_ MSG message

Let's look at message processing. In message processing, we mainly call the disablekeyguard function of WMS.

2、 After fixing the screen, the activity starts the process

After fixing the screen, if we start other taskrecord activities, we can't start them. Let's take a look at this principle. In the startactivityuncheckedlocked function, the islocktaskmodeviolation function will be called to determine whether to further start the activity process. Let's take a look at this function and call getlockedtasklocked to see mlocktaskmodetasks (those tasks that lock the screen). If the current task is the task that is currently fixing the screen, Directly return false means that you can continue to start the activity process. If not, we need to look at the mlocktaskauth variable of the task.

Let's look at the setLockedTaskAuth function of TaskRecord again. When we build a new TaskRecord, we call the setIntent function, and the setIntent function is called in the TaskRecord constructor. Let's see that the value of this function mlocktaskauth is determined according to mlocktaskmode, which is passed in by activityinfo. This value is constructed when PKMs parses androidmanifest.xml. The default is lock_ TASK_ LAUNCH_ MODE_ Default. When there is no white list, mlocktaskauth is lock_ TASK_ AUTH_ PINNABLE。

Let's take another look at the following code of the islocktaskmodeviolation function. The mlocktaskauth of the task is lock_ TASK_ AUTH_ Pinnable, which is currently on a fixed screen, so mlocktaskmodetasks is not null, and finally returns true. Then the activity startup process cannot go on, that is, it means that starting an ordinary activity will be blocked.

3、 Unpin screen

Finally, let's take a look at the cancel fixed screen. The cancel screen will be cancelled in the phonestatusbar, but there must be a virtual key. That's how the native is set. Finally, the stopLockTaskModeOnCurrent function of AMS is invoked. This function mainly calls the stoplocktaskmode function. In this function, it mainly calls the setlocktaskmodelocked function of activitystacksupervisor. It was also called when fixing the screen, but here we look carefully. The first parameter is null.

Let's look at this function. If it's empty, now call getLockedTaskLocked to get the TaskRecord of the current fixed screen, and then call removeLockedTaskLocked to remove the TaskRecord. If it is not null, then call resumeTopActivitiesLocked to start the next Activity (which is the Activity of the next screen locked TaskRecord). If it is empty, return directly. But it will return to normal the next time we start a normal activity, because mlocktaskmodetasks is empty.

4、 How to cancel screen fixation without virtual key

As mentioned earlier, if there is no virtual key, you can't cancel the screen fixation. Let's talk about the following ways

1. Use the AM command am task lock stop to call the stoplocktaskmode function of am

2. Alternatively, we can modify the code in activity.java and press the return key for a long time to call the stoplocktaskmode method of AMS. The following is the implementation. The stoplocktask provided by activity itself calls the stoplocktaskmode method of AMS

3. Ash this item directly in settings

Security will be read in securitysettings_ settings_ Misc.xml file, and then add the relevant privilege, which will have the following fixed screen related

After reading the file in SecuritySettings, we can call the hasNavigationBar of WMS to see if there is any virtual key (no virtual key can not be canceled when the screen is fixed), if we do not directly put the Settings in the ash.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>