[Android] Lock screen

Source: Internet
Author: User

Recently played more Huanhuan, owes a lot of things did not write. Today, simply fill in a simple piece of content. Is recently a design that involves locking the Android device screen, probably through a button or service message, to control the device interface is completely locked, cannot click anything. Feeling that this need may involve a lot of things, but do not find ... The content is very simple, follow the code quickly over again.

This address : http://www.cnblogs.com/rossoneri/p/4409691.html

The main interface has a button to trigger the event:

Press the button to display another lock screen, plus an unlock button (if not added, how to quit the program AH-!) )

As you can see, the status bar at the top of the boot lock screen disappears completely, and the navigation bar button below fades into small dots. At this point you click on the screen anywhere the program will have no reaction. Okay, here's the code.

Lock Screen class Lockscreen.java:

 PackageCom.example.wow.demo_lockscreen;ImportAndroid.content.Context;ImportAndroid.graphics.PixelFormat;ImportAndroid.graphics.Point;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;/*** Created by Wow on 15-4-9.*/ Public classlockscreen {point mlpsize;    Button Mbtnunlock;    ViewGroup MView; FinalWindowManager Mwindowmanager; FinalWindowmanager.layoutparams mLp;  Publiclockscreen (Context mcontext) {MView= (ViewGroup) layoutinflater.from (Mcontext). Inflate (R.layout.lock_screen_view,NULL); Mwindowmanager=(WindowManager) Mcontext.getsystemservice (Context.window_service); MLp=NewWindowmanager.layoutparams (); Mbtnunlock=(Button) Mview.findviewbyid (R.id.btn_unlock); Mbtnunlock.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Show (false);        }        });    INITLP (); }    Private voidINITLP () {Mlp.type= WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;//Type_system_alert;Mlp.format =pixelformat.rgba_8888; Mlp.flags|=WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_FULLSCREEN|WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; Mlp.systemuivisibility=View.status_bar_hidden; Mlp.width=WindowManager.LayoutParams.MATCH_PARENT; Mlp.height=WindowManager.LayoutParams.MATCH_PARENT; }     Public voidShowBooleanflag) {        if(flag) {Mwindowmanager.addview (MView, mLp); } Else{Mwindowmanager.removeview (MView); }    }}

The point here is

Mlp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

In this sentence, type must be set to this type, the function of this type is to let the currently displayed view no reason to appear at the top. Flags are set according to needs, can add other content, can also delete some, does not affect.

You also need to add permissions to the manifest.

<android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

Mainactivity:

 PackageCom.example.wow.demo_lockscreen;Importandroid.app.Activity;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Button mbtnlock;    Lockscreen Mlockscreen; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mbtnlock=(Button) Findviewbyid (R.id.btn_lock); Mlockscreen=NewLockscreen ( This); Mbtnlock.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {mlockscreen.show (true);    }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

The interface file is not posted, too simple. Oh yes, that's what I measured in 5.0 is the perfect run, 4.2 a bit of a problem, not quite clear why. Let me know.

Well, the lock screen is actually so simple. Later I will study some of the interface effect, do some practical lock screen function.

Recently in the study of system bar, quite simple, before writing a few related blog, found before understanding or a little deviation, intends to write a final version of this piece of knowledge to fix.

[Android] Lock screen

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.