android4.0 中怎么在app 中拦截HOME键

android4.0 中如何在app 中拦截HOME键

转自:http://blog.csdn.net/fulinwsuafcie/article/details/7717408

 近日在研究一个视频锁的功能,即在 视频播放界面上设一个锁的功能,当该锁起效后,就会屏蔽back, home, menu三个键的功能。

back 和 menu 键都可以通过 app层  的 onKeyDown 和 onKeyUp 函数来拦截。但是 home 键不行。

于是上网查了一下在app层屏蔽 home 键的方法。

主流的方法如下所示。但只在2.2,2.3平台下有效。(下面列举的方法只在android 2.2, 2.3 上有效)

屏蔽Activity, Dialog风格Activity, AlertDialog的Home键功能方法分别是:

1.屏蔽Activity中的Home键功能,只需要在你要屏蔽的activity 中重写 onAttachToWindow() 这个函数就可以了

[java] view plaincopy
 
  1. @Override    
  2. public void onAttachedToWindow() {    
  3.     this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
  4.     super.onAttachedToWindow();    
  5. }    


 

2.屏蔽 Dialog风格Activity的Home键功能,也需要在你要屏蔽的activity 中重写 onAttachToWindow() 这个函数,但设置window 的值需要改变为 TYPE_KEYGUARD_DIALOG

[java] view plaincopy
 
  1. @Override    
  2. public void onAttachedToWindow() {    
  3.     this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);   
  4.     super.onAttachedToWindow();    
  5. }    



这里之所以和Activity区分,是因为Dialog风格Activity如果用第一种方法,这个Dialog风格Activity的背景就变成黑色的,而不是透明的.

3.屏蔽AlertDialog的Home键功能

[java] view plaincopy
 
  1. AlertDialog d = b.create();  
  2. d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  


具体原理,可以参看 http://blog.csdn.net/wdaming1986/article/details/7539600 。上面讲的比较清楚,这里就不再赘述了。

 

后面通过上面一篇文章的思路来看 4.0平台的 framework 层的代码,发现在 PhoneWindowManager.java 的 interceptKeyBeforeDispatching 函数中对home键的处理如下:

[java] view plaincopy
 
  1. if (keyCode == KeyEvent.KEYCODE_HOME) {  
  2.     // If we have released the home key, and didn't do anything else  
  3.     // while it was pressed, then it is time to go home!  
  4.     if (mHomePressed && !down) {  
  5.         mHomePressed = false;  
  6.           
  7. ..  
  8.           
  9.     }  
  10.   
  11.     // If a system window has focus, then it doesn't make sense  
  12.     // right now to interact with applications.  
  13.     WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;  
  14.     if (attrs != null) {  
  15.         final int type = attrs.type;  
  16.         if (type == WindowManager.LayoutParams.TYPE_KEYGUARD  
  17.                 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {  
  18.             // the "app" is keyguard, so give it the key  
  19.             return 0;  
  20.         }  
  21.         final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;  
  22.         for (int i=0; i<typeCount; i++) {  
  23.             if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {  
  24.                 // don't do anything, but also don't pass it to the app  
  25.                 return -1;  
  26.             }  
  27.         }  
  28.           
  29.         final int flag = attrs.flags;  
  30.   
  31.         if ((flag & WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED) != 0) {  
  32.             // the window wants to handle the home key, so dispatch it to it.  
  33.             return 0;  
  34.         }                  
  35.     }  
  36.       
  37.     return -1;  

 

在此函数中,如果返回 0,则 home 键会交给 app 来处理,如果返回 -1 ,则不会处理 home 键。

在app 的你要屏蔽home 键的activity 中,只需要添加该标志就可以了

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

添加了该标志以的,HOME键就被 app 层接管了,可以在 app 层代码的 onKeyDown 或 onKeyUp 函数中来处理 HOME 键了。

如果要使home 键有效,再 clearFlags 即可。

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

 

从上面的代码来看,应该设置 window 的类型也是有效的,但不知为何在 4.0 中不取作用,打Log 看也不取效。不知何故。

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

1 楼 北极光之吻 2014-01-10  
http://862123204-qq-com.iteye.com/blog/1888532