Android 4.0 shields the Home Key

Source: Internet
Author: User

According to the general method, the general method is as follows:

Java code
  1. Public void onattachedtowindow ()
  2. {
  3. This. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
  4. Super. onattachedtowindow ();
  5. }

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
  1. Public static final int type_system_error
  2. Since: API Level 1
  3. 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
  1. Public class locklayer {
  2. Private activity mactivty;
  3. Private windowmanager mwindowmanager;
  4. Private view mlockview;
  5. Private layoutparams mlockviewlayoutparams;
  6. Public locklayer (Activity Act ){
  7. Mactivty = Act;
  8. Init ();
  9. }
  10. Private void Init (){
  11. Mwindowmanager = mactivty. getwindowmanager ();
  12. Mlockviewlayoutparams = new layoutparams ();
  13. Mlockviewlayoutparams. width = layoutparams. match_parent;
  14. Mlockviewlayoutparams. Height = layoutparams. match_parent;
  15. // Key to implementation
  16. Mlockviewlayoutparams. type = layoutparams. type_system_error;
  17. // Apktool value. Which variable is the specific value? Please help me
  18. Mlockviewlayoutparams. Flags = 1280;
  19. }
  20. Public void lock (){
  21. If (mlockview! = NULL ){
  22. Mwindowmanager. addview (mlockview, mlockviewlayoutparams );
  23. }
  24. }
  25. Public void unlock (){
  26. If (mwindowmanager! = NULL ){
  27. Mwindowmanager. removeview (mlockview );
  28. }
  29. }
  30. Public void setlockview (view v ){
  31. Mlockview = V;
  32. }
  33. }

The usage is as follows:

Java code
  1. Public void oncreate (bundle savedinstancestate ){
  2. Super. oncreate (savedinstancestate );
  3. View lock = view. Inflate (this, R. layout. Main, null );
  4. Locklayer = new locklayer (this );
  5. Locklayer. setlockview (Lock );
  6. Locklayer. Lock ();
  7. }

 

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
  1. Public class locklayer {
  2. Private activity mactivty;
  3. Private windowmanager mwindowmanager;
  4. Private view mlockview;
  5. Private layoutparams mlockviewlayoutparams;
  6. Private Static locklayer mlocklayer;
  7. Private Boolean islocked;
  8. Public static synchronized locklayer getinstance (Activity Act ){
  9. If (mlocklayer = NULL ){
  10. Mlocklayer = new locklayer (Act );
  11. }
  12. Return mlocklayer;
  13. }
  14. Private locklayer (Activity Act ){
  15. Mactivty = Act;
  16. Init ();
  17. }
  18. Private void Init (){
  19. Islocked = false;
  20. Mwindowmanager = mactivty. getwindowmanager ();
  21. Mlockviewlayoutparams = new layoutparams ();
  22. Mlockviewlayoutparams. width = layoutparams. match_parent;
  23. Mlockviewlayoutparams. Height = layoutparams. match_parent;
  24. // Key to implementation
  25. Mlockviewlayoutparams. type = layoutparams. type_system_error;
  26. // Apktool value. Which variable is the specific value? Please help me
  27. Mlockviewlayoutparams. Flags = 1280;
  28. }
  29. Public synchronized void lock (){
  30. If (mlockview! = NULL &&! Islocked ){
  31. Mwindowmanager. addview (mlockview, mlockviewlayoutparams );
  32. }
  33. Islocked = true;
  34. }
  35. Public synchronized void unlock (){
  36. If (mwindowmanager! = NULL & islocked ){
  37. Mwindowmanager. removeview (mlockview );
  38. }
  39. Islocked = false;
  40. }
  41. Public synchronized void setlockview (view v ){
  42. Mlockview = V;
  43. }
  44. }

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

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.