Android4.0 custom lock screen summary [Android lock screen Research 1]

Source: Internet
Author: User

Recently, I moved from Beijing to Shenzhen. Except for the weather, the other jobs are similar and work is similar! To commemorate your migration.

----- Digress

Reprinted to indicate the source: Http://blog.csdn.net/wdaming1986/article/details/8837023

Well, let's get down to the truth and talk about the lock screen. In fact, making the lock screen into the APK form will cause many problems and cause unnecessary troubles, like the popular go locks and 91 locks on the market, there will also be some problems, but they will not affect much. The best way is to modify the source code and customize your own locks in the source code, so that they can be used once and for all;

The following four things should be taken into account for screen lock;

(1) Replace the screen lock of the system, so that the screen lock of the system cannot be displayed;

(2) shield the home keyboard, back key, and menu key;

(3) On other interfaces or launcher interfaces, long press the Home Key and cannot display your lock APK in the latest task;

(4) display your lock screen at startup

 

The answer to this 4.11 is not necessarily the complete code. It only provides the following ideas. If you are interested, you can ask questions;

For (1 ),This is relatively easy. You can call disablekeyguard to drop the system;

mKeyguardManager = (KeyguardManager)Class.this.getSystemService(Context.KEYGUARD_SERVICE);mKeyguardLock = mKeyguardManager.newKeyguardLock("my_lockscreen"); mKeyguardLock.disableKeyguard();

Note:: Add permissions to manifext. xml:

<Uses-Permission Android: Name = "android. Permission. disable_keyguard"/>

For (2 ),For how to handle this home key, I think some people write a blog on the Internet. Some people do this:

Public class locklayer {private activity mactivty; private windowmanager mwindowmanager; private view mlockview; private layoutparams mlockviewlayoutparams; Private Static locklayer mlocklayer; private Boolean islocked; public static synchronized locklayer getinstance (Activity Act) {If (mlocklayer = NULL) {mlocklayer = new locklayer (ACT);} return mlocklayer;} private locklayer (Activity Act) {Ma Ctivty = Act; Init ();} private void Init () {islocked = false; mwindowmanager = mactivty. getwindowmanager (); mlockviewlayoutparams = new layoutparams (); mlockviewlayoutparams. width = layoutparams. match_parent; mlockviewlayoutparams. height = layoutparams. match_parent; // implement the key mlockviewlayoutparams. type = layoutparams. type_system_error; // apktool value. Which variable is the specific value? Please help mlockviewlayoutparams. flags = 1 280;} public synchronized void lock () {If (mlockview! = NULL &&! Islocked) {mwindowmanager. addview (mlockview, mlockviewlayoutparams);} islocked = true;} public synchronized void unlock () {If (mwindowmanager! = NULL & islocked) {mwindowmanager. removeview (mlockview);} islocked = false;} public synchronized void setlockview (view v) {mlockview = V ;}}

Principle: this view is regarded as a system error view,

This is also based on the interceptkeybeforedispatching () method in phonewindowmanager. java. Please check the truth:

final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;                for (int i=0; i<typeCount; i++) {                    if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {                        // don't do anything, but also don't pass it to the app                        return -1;                    }                }

NOTE: If one of the two attributes is set, no processing is performed. The Home key does not take effect for this app;

Window_types_where_home_doesnt_work data is defined as follows:

private static final int[] WINDOW_TYPES_WHERE_HOME_DOESNT_WORK = {            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,        };

Now you can understand why the above Code is set to type_system_error. In fact, it can be set to type_system_alert.

Let's take a look.The SDK documentation provides explanations,

Window Type: internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.

The error view is at the top of all views, so the Home key can be blocked;

The problem is: Long press the power key on this interface, and the shutdown interface cannot be displayed. I have tried this, so this method is not available;

Some people say this:

Private Static final int flag_homekey_dispatched = 0x80000000;

In the activity of the app that you want to block the Home key, you only need to add this flag to this. getwindow (). setflags (flag_homekey_dispatched, flag_homekey_dispatched); if you want to make the Home Key valid, then clearflags. Getwindow (). clearflags (windowmanager. layoutparams. flag_homekey_dispatched );

Note:The above code must be added before setcontentview;

This code is derived from the interceptkeybeforedispatching () method of the phonewindowmanager. Java class,

 if ((flag & WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED) != 0) {                    // the window wants to handle the home key, so dispatch it to it.                    return 0;                }    

In this way, some users feel that they are easy to use and some others feel that they are not easy to use;

Cause:The above code is not added to phonewindowmanager. Java of android4.0 source code, so it does not work,

The 4.1 source code phonewindowmanager. Java contains this code, so it is easy to use. Please check whether the corresponding source code has the above Code;


For (3 ),This simulates go lock screen and 91 lock screen, and configures

<category android:name="android.intent.category.HOME" />

In this case, first clear the default settings for system startup, that is, select the home that the system clicks the Home key to start, set it to start its own screen lock, and then set the desktop to start after unlocking, the logic can be handled by yourself. The general idea is like this;


For (4 ),This go lock screen is not ideal for 91 lock screen processing. I thought of a good idea to update it later. The general idea is to listen to the boot broadcast, <action Android: Name = "android. intent. action. boot_completed "/>, this is to write a mybootcompletedreceiver class, the role is to disable the screen lock of the system, then start your own screen lock service, listen to the screen bright and off broadcast, dynamic Monitoring is required for the bright and extinct broadcast. Do you have good ideas or opinions to leave a message to discuss?

Be a passionate programmer!

Related Article

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.