According to the general method, the general method is as follows:
Java code
- Public void onattachedtowindow ()
- {
- This. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
- Super. onattachedtowindow ();
- }
However, this method becomes invalid when it reaches 4.0 of the system, and the desktop will still be returned if you press home. Apktool has several lock screen software and finds its implementation method. It adds the view to the window using the addview method of windowmanager, and sets the layoutparamas type of the view to layoutparams. type_system_error. the SDK value is interpreted
Java code
- Public static final int type_system_error
- Since: API Level 1
- Window Type: internal system error windows, appear on top of everything they can.
If this method is used directly, an error will be reported, and the error will be reported in androidmanifest. add the permission <uses-Permission Android: Name = "android. permission. system_alert_window "/>, complete code on OK, encapsulates a class, where the lock and hide methods implement lock and unlock.
Java code
- Public class locklayer {
- Private activity mactivty;
- Private windowmanager mwindowmanager;
- Private view mlockview;
- Private layoutparams mlockviewlayoutparams;
- Public locklayer (Activity Act ){
- Mactivty = Act;
- Init ();
- }
- Private void Init (){
- Mwindowmanager = mactivty. getwindowmanager ();
- Mlockviewlayoutparams = new layoutparams ();
- Mlockviewlayoutparams. width = layoutparams. match_parent;
- Mlockviewlayoutparams. Height = layoutparams. match_parent;
- // Key to implementation
- Mlockviewlayoutparams. type = layoutparams. type_system_error;
- // Apktool value. Which variable is the specific value? Please help me
- Mlockviewlayoutparams. Flags = 1280;
- }
- Public void lock (){
- If (mlockview! = NULL ){
- Mwindowmanager. addview (mlockview, mlockviewlayoutparams );
- }
- }
- Public void unlock (){
- If (mwindowmanager! = NULL ){
- Mwindowmanager. removeview (mlockview );
- }
- }
- Public void setlockview (view v ){
- Mlockview = V;
- }
- }
The usage is as follows:
Java code
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- View lock = view. Inflate (this, R. layout. Main, null );
- Locklayer = new locklayer (this );
- Locklayer. setlockview (Lock );
- Locklayer. Lock ();
- }
OK. At this time, you can press the Home key without returning the desktop effect. Other keys need to be implemented using another code, which is available on the Internet. This code is called only once. However, if you encounter multiple threads or multiple locks and hide, there will be a problem. Modify the locklayer to the singleton mode as follows, the second is to judge the status when using hide and lock. The modified code is as follows:
Java code
- 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 ){
- Mactivty = Act;
- Init ();
- }
- Private void Init (){
- Islocked = false;
- Mwindowmanager = mactivty. getwindowmanager ();
- Mlockviewlayoutparams = new layoutparams ();
- Mlockviewlayoutparams. width = layoutparams. match_parent;
- Mlockviewlayoutparams. Height = layoutparams. match_parent;
- // Key to implementation
- Mlockviewlayoutparams. type = layoutparams. type_system_error;
- // Apktool value. Which variable is the specific value? Please help me
- Mlockviewlayoutparams. Flags = 1280;
- }
- 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;
- }
- }
Another problem is that mlockviewlayoutparams. Flags = 1280; 1280 does not know which variable the value belongs to. If you know it, leave a message.
- Lockscreen.zip (148.2 KB)
- Downloads: 0