1. Add this code in the activity to shield the home Key (the onKeyDown event captures the home key ).
Public void onAttachedToWindow ()
{
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
Super. onAttachedToWindow ();
}
Public void onAttachedToWindow ()
{
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
Super. onAttachedToWindow ();
} 2. Because the android system processes the power key with the home Key in PhoneWindowManager, it will not return to the upper-layer application. The following is the system source code:
\ Frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ PhoneWindowManager. java row 1089
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;
}
}
}
\ Frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ PhoneWindowManager. java row 1089
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;
}
}
}
Type = WindowManager. layoutParams. in the TYPE_KEYGUARD clause, we can see that android has made a special judgment on the screen lock, so I simulate this implementation. The only difference is that the onAttachedToWindow () method in the activity requires more than 5 APIs.
From the column xiaoxiaobian3310903