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:
- @ Override
- Public Boolean onkeydown (INT keycode, keyevent event ){
- Log. I (TAG, "keycode =" + keycode + "isban =" + isban );
- Switch (keycode ){
- Case keyevent. keycode_back:
- Log. I (TAG, "keycode_back ");
- Return true;
- }
- Return super. onkeydown (keycode, event );
- }
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:
- // First we always handle the Home key here, So applications
- // Can never break it, although if keyguard is on, we do let
- // It handle it, because that gives us the correct 5 second
- // Timeout.
- If (code = keyevent. keycode_home ){
- // If a system window has focus, then it doesn't make sense
- // Right now to interact with applications.
- Windowmanager. layoutparams attrs = win! = NULL? Win. getattrs (): NULL;
- If (attrs! = NULL ){
- Final int type = attrs. type;
- If (type = windowmanager. layoutparams. type_keyguard
- | Type = windowmanager. layoutparams. type_keyguard_dialog ){
- // The "app" is keyguard, so give it the key
- Return false;
- }
- Final int typecount = window_types_where_home_doesnt_work.length;
- For (INT I = 0; I <typecount; I ++ ){
- If (type = window_types_where_home_doesnt_work [I]) {
- // Don't do anything, but also don't pass it to the app
- Return true;
- }
- }
- }
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:
- Public void onattachedtowindow (){
- This. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
- Super. onattachedtowindow ();
- }
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:
- Final dialog = new dialog (this );
- Dialog. setcontentview (R. layout. mydailog );
- Dialog. getwindow (). settype (windowmanager. layoutparams. type_keyguard );
- Dialog. Show ();
- Dialog. setonkeylistener (new Android. content. dialoginterface. onkeylistener (){
- @ Override
- Public Boolean onkey (dialoginterface dialog, int keycode, keyevent event ){
- Switch (keycode ){
- Case keyevent. keycode_back:
- Log. I (TAG, "keycode_back ");
- Return true;
- }
- Return false;
- }
- });
An error occurs as follows:
Java code:
- 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:
- Dialog. Show ();
- Dialog. getwindow (). settype (windowmanager. layoutparams. type_keyguard );