Android4.0 shield the Home Key

Source: Internet
Author: User

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.

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.