[Android instance] the android activity shields the Home Key.

Source: Internet
Author: User

From http://www.eoeandroid.com/thread-109270-1-1.html

You have read many articles about the general method to block the Home key. To sum up, let's first talk about the screen button and Home Key of the activity.

Shield other keys and override onkeydown

Java code:

  1. @ Override
  2. Public Boolean onkeydown (INT keycode, keyevent event ){
  3. Log. I (TAG, "keycode =" + keycode + "isban =" + isban );
  4. Switch (keycode ){
  5. Case keyevent. keycode_back:
  6. Log. I (TAG, "keycode_back ");
  7. Return true;
  8. }
  9. Return super. onkeydown (keycode, event );
  10. }

Blocking the Home key is not captured here, because the permissions are generally user, so it is invalid.
In fact, Android processes system-level buttons such as the Home Key.

Java code:

  1. // First we always handle the Home key here, So applications
  2. // Can never break it, although if keyguard is on, we do let
  3. // It handle it, because that gives us the correct 5 second
  4. // Timeout.
  5. If (code = keyevent. keycode_home ){
  6. // If a system window has focus, then it doesn't make sense
  7. // Right now to interact with applications.
  8. Windowmanager. layoutparams attrs = win! = NULL? Win. getattrs (): NULL;
  9. If (attrs! = NULL ){
  10. Final int type = attrs. type;
  11. If (type = windowmanager. layoutparams. type_keyguard
  12. | Type = windowmanager. layoutparams. type_keyguard_dialog ){
  13. // The "app" is keyguard, so give it the key
  14. Return false;
  15. }
  16. Final int typecount = window_types_where_home_doesnt_work.length;
  17. For (INT I = 0; I <typecount; I ++ ){
  18. If (type = window_types_where_home_doesnt_work [I]) {
  19. // Don't do anything, but also don't pass it to the app
  20. Return true;
  21. }
  22. }
  23. }

Through the source code, we can easily find two parameters: windowmanager. layoutparams. type_keyguard and
Windowmanager. layoutparams. type_keyguard_dialog uses this reference to override onattachedtowindow to shield the Home Key.

Java code:

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

It is the turn of dialog. If the dialog is displayed in the activity, the preceding two methods cannot be blocked in the activity.
In fact, the principle is the same, but the place is different.

Java code:

  1. Final dialog = new dialog (this );
  2. Dialog. setcontentview (R. layout. mydailog );
  3. Dialog. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
  4. Dialog. Show ();
  5. Dialog. setonkeylistener (new Android. content. dialoginterface. onkeylistener (){
  6. @ Override
  7. Public Boolean onkey (dialoginterface dialog, int keycode, keyevent event ){
  8. Switch (keycode ){
  9. Case keyevent. keycode_back:
  10. Log. I (TAG, "keycode_back ");
  11. Return true;
  12. }
  13. Return false;
  14. }
  15. });

An error occurs as follows:

Java code:

  1. 1.10-18 13:27:06. 380: Error/androidruntime (4684): caused by: Android. view. windowmanager $ badtokenexception: unable to add window android. view. viewroot $ W @ 2b046d68 -- permission denied for this window type

In fact, you only need to put the location of dialog. getwindow (). settype behind show.

Java code:

  1. Dialog. Show ();
  2. Dialog. getwindow (). settype (windowmanager. layoutparams. type_keyguard );

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.