How can I block the HOME Key in the app in android4.0?

Source: Internet
Author: User

Recently, we have been studying the function of a video lock, that is, setting a lock on the video playback interface. When the lock becomes effective, it will shield back, home, menu.

Both the back and menu keys can be intercepted through the onKeyDown and onKeyUp functions at the app layer. But the home key does not work.

So I checked the method of shielding the home key on the app layer.

The mainstream methods are as follows. It is only valid on platforms 2.2 and 2.3. (The method listed below is only valid on android 2.2 and 2.3)

The Home key function for shielding Activity, Dialog style Activity, and AlertDialog is as follows:

1. Block the Home key function in the Activity. You only need to override onAttachToWindow () in the activity you want to shield.


[Java]
@ Override
Public void onAttachedToWindow (){
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
Super. onAttachedToWindow ();
}
@ Override
Public void onAttachedToWindow (){
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
Super. onAttachedToWindow ();
}

 

2. Block the Home key function of the Dialog style Activity. You also need to re-write the onAttachToWindow () function in the activity you want to shield, but set the window value to TYPE_KEYGUARD_DIALOG.


[Java]
@ Override
Public void onAttachedToWindow (){
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD_DIALOG );
Super. onAttachedToWindow ();
}
@ Override
Public void onAttachedToWindow (){
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD_DIALOG );
Super. onAttachedToWindow ();
}
 


The reason for distinguishing this from Activity is that if the Dialog style Activity uses the first method, the background of the Dialog style Activity will become black rather than transparent.

3. Shielding AlertDialog's Home key function


[Java]
AlertDialog d = B. create ();
D. getWindow (). setType (WindowManager. LayoutParams. TYPE_SYSTEM_ALERT );
AlertDialog d = B. create ();
D. getWindow (). setType (WindowManager. LayoutParams. TYPE_SYSTEM_ALERT );

Later, based on the idea in the previous article, we will look at the framework layer code of the 4.0 platform and find that the home Key is processed in the interceptKeyBeforeDispatching function of PhoneWindowManager. java as follows:

[Java]
If (keyCode = KeyEvent. KEYCODE_HOME ){
// If we have released the home key, and didn't do anything else
// While it was pressed, then it is time to go home!
If (mHomePressed &&! Down ){
MHomePressed = false;

..

}
 
// 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 0;
}
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-1;
}
}

Final int flag = attrs. flags;
 
If (flag & WindowManager. LayoutParams. FLAG_HOMEKEY_DISPATCHED )! = 0 ){
// The window wants to handle the home key, so dispatch it to it.
Return 0;
}
}

Return-1;
If (keyCode = KeyEvent. KEYCODE_HOME ){
// If we have released the home key, and didn't do anything else
// While it was pressed, then it is time to go home!
If (mHomePressed &&! Down ){
MHomePressed = false;

......

}

// 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 0;
}
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-1;
}
}

Final int flag = attrs. flags;

If (flag & WindowManager. LayoutParams. FLAG_HOMEKEY_DISPATCHED )! = 0 ){
// The window wants to handle the home key, so dispatch it to it.
Return 0;
}
}

Return-1;
}

 
 

In this function, if 0 is returned, the home Key is handed over to the app for processing. If-1 is returned, the home key is not processed.

In the activity of the app that you want to block the home key, you only need to add this flag.

GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_HOMEKEY_DISPATCHED );

If you want to make the home Key valid, then clearFlags.

GetWindow (). clearFlags (WindowManager. LayoutParams. FLAG_HOMEKEY_DISPATCHED );

 

From the code above, the window type should also be set to be valid, but I do not know why it does not work in 4.0, and it does not take effect when logging. Somehow.

This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );

Author: fulinwsuafcie
 

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.