android4.0+ Lock Screen Program development--Key shielding Chapter

Source: Internet
Author: User

The key issue that we are facing when developing the lock screen program is undoubtedly how to block three keys, Back,home,menu

Seemingly simple functions are not so easy to implement.

"Shield Back Button"

Relatively speaking, shielding the back key is relatively simple, only need to rewrite the onkeydown method in our activity, and in which the back button interception can be, the code is as follows

  

  // Shielding Back     @Override    publicboolean onKeyDown (int  keycode, KeyEvent event) {         Switch (keycode) {            case  keyevent.keycode_back:                returnTrue ;        }         return Super . OnKeyDown (KeyCode, event);    }

"Mask menu Button"

Shielding the menu button may seem to be the same as the Back button processing method, but in fact is not so, because in more than 4.0 of the system, in most cases, click the menu button will appear recent app page.

So using the above method doesn't work.

At this point, we can use another method--to detect whether our form loses focus, and if so, to indent the popup form back. This method can also prevent the drop-down status bar

The code is as follows

   // Mask Menu     @Override    publicvoid onwindowfocuschanged (boolean  Phaswindowfocus) {        super. onwindowfocuschanged (Phaswindowfocus);         if (! Phaswindowfocus) {            sendbroadcast (new  Intent (intent.action_close_system_dialogs));        }    }

"Shield Home Button"

Next we have to face the most complex part, shielding home button, home button due to its particularity, can not be intercepted.

However, we can refer to other lock screen applications, set up a home screen application, so that each time you click on the Home button to launch our own home screen, and then to determine whether to continue to stay on the lock screen or start the system's main screen page.

Our main screen interface activity should be no form, so that we do not see the extra form when we click on home, so that the user experience better.

Let's use the code to explain it.

 Public classHomeextendsActivity {homechoice homechoice;//HomeChoice to set up and start the Home screen class    Privatesharedpreferences sharedpreferences; PrivateEditor Editor; Private BooleanIslock; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); HomeChoice=NewHomeChoice ( This); Sharedpreferences= Getsharedpreferences ("HomeChoice", mode_private); } @Overrideprotected voidOnStart () {Super. OnStart (); Islock= Sharedpreferences.getboolean ("IsLocked",false); //determine if the lock screen activity is in the foreground        if(islock) {Intent mintent=NewIntent ( This, Screenlockactivity.class);            StartActivity (mintent);        Finish (); }        //Do not start the default home screen at the front desk        Else {            Try{homechoice.originalhome ();//Start the default home screen}Catch(Exception e) {homechoice.choosebackhome ();//There is no preset to allow users to preset            }        }    }}

In order for this interface to be started when you press home, we need to set it in Androidmanifest, as follows

<activity            android:name= ". Home "            android:theme=" @android: Style/theme.nodisplay "//Set theme is not visible            >            <intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.HOME "/>//can be recognized as the desktop                <category android:name=" Android.intent.category.DEFAULT "/ >            </intent-filter>        </activity>

We also need to set our app as the main screen in the system setup.

For example, this can be set in a native system

This can be set in MIUI

Other app management--default app settings--desktop

With the above steps, we can complete the shielding of the home button.

The above code just to let everyone better understand how to block the principle of the button, not complete, I have a complete demo code uploaded to GitHub above, if you need to go down and try.

Address: Https://github.com/u3shadow/ScreenLockTest

Recently, because of the lock screen involved in the development, so looked for a long time this information, but are not perfect, very painful. I hope this article can help everyone.

android4.0+ Lock Screen Program development--Key shielding Chapter

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.