天天看点

屏蔽home键

 相信在Android应用上,很多时候逻辑是需要屏蔽Home键的,但这个用户体验是否需要,就看各位的需求了。 

    一般的方法屏蔽Home键,大家一定看过不少文章了。我总结一下,先说一下一般情况下Activity的屏蔽按键和Home键吧。 

屏蔽其他键,重写onKeyDown 

屏蔽home键

@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);  

}  

大家会发现,这里屏蔽Home键是捕捉不到的,因为大家的权限一般是User所以是无效的。 

而其实android处理Home键等系统级按键是有一定的处理的。 

引用

看看源码是怎样处理的 \frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java #1092

屏蔽home键

// 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;  

            }  

通过源码,我们不难发现两个的参数 WindowManager.LayoutParams.TYPE_KEYGUARD和 

WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG 

借鉴于此,重写onAttachedToWindow,以实现屏蔽Home键

屏蔽home键

public void onAttachedToWindow() {  

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

    super.onAttachedToWindow();  

- - - - - - - - - - - - -  - - - - - - - - - - - - - - - - 华丽的分界线,以下内容更精彩- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

轮到dialog了,如果在Activity弹出dialog,在Activity设置以上2个方法是没办法屏蔽的。 

其实,原理是一样的,只是地方不一样而已。 

屏蔽home键

final Dialog 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;  

});   

这样运行后,出错如下: 

屏蔽home键

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  

其实,只需要把dialog.getWindow().setType的位置放在show后面就可以了 

屏蔽home键

这么,就完成了Back键的屏蔽 和Home键盘的屏蔽了! 

总结: 

1:)在以上用WindowManager.LayoutParams.TYPE_KEYGUARD的地方改用 

WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG 效果一样。至于两者的具体差别,得以后再研究研究。 

2:)其实,在源码里是这样调用的。

屏蔽home键

final AlertDialog dialog = new AlertDialog.Builder(mContext)  

    .setTitle(null)  

    .setMessage(message)  

    .setNeutralButton(R.string.ok, null)  

    .create();  

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);  

    但我们如果这样调用就会出现之前的那个error:permission denied for this window type 这就显而易见了吧~~ 

3:)ProgressDialog 默认屏蔽 Back键,Dialog,AlertDialog则需setOnKeyListener 

4:)其实屏蔽Home键,在页面的某个地方,例如一个Button的onClick里,去设置setType就可以了,如: 

屏蔽home键

button.setOnClickListener(new View.OnClickListener() {  

    public void onClick(View v) {  

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

});  

但前提是重载Activity的onAttachedToWindow(),哪怕只是一个空实现,然后返回父类方法。 

屏蔽home键

@Override    

5:)其实它们,都是常用的~

屏蔽home键

switch (keyCode) {  

    case KeyEvent.KEYCODE_HOME:  

        Log.i(TAG,"KEYCODE_HOME");  

    case KeyEvent.KEYCODE_BACK:  

    case KeyEvent.KEYCODE_CALL:  

        Log.i(TAG,"KEYCODE_CALL");  

    case KeyEvent.KEYCODE_SYM:  

        Log.i(TAG,"KEYCODE_SYM");  

    case KeyEvent.KEYCODE_VOLUME_DOWN:  

        Log.i(TAG,"KEYCODE_VOLUME_DOWN");  

    case KeyEvent.KEYCODE_VOLUME_UP:  

        Log.i(TAG,"KEYCODE_VOLUME_UP");  

    case KeyEvent.KEYCODE_STAR:  

        Log.i(TAG,"KEYCODE_STAR");  

希望大家看到这个文章能觉得有用,谢谢已阅者! 

2011-10-20 更新如下: 

总结1:)的问题,有答案了,时间问题我就简单写写吧: 

    从功能上来说,是一样的,区别在样式。 

    如果你喜欢用Theme.Dialog去把一个Activity装饰成一个Dialog去显示,你会发现。 

屏蔽home键

android:theme="@android:style/Theme.Dialog"  

背景是透明的。 

如果在

屏蔽home键

setTheme(android.R.style.Theme_Dialog);  

背景则是黑色的。 

这是为什么呢?。。。我不知道。 

治标不治本的方法来了!若你在Activity重写onAttachedToWindow 

屏蔽home键

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

那么出来的效果,就是透明背景的dialog了,当然前提是你需要实现屏蔽Home键。至于其中到底哪一代码导致样式改变呢,那就以后再去看源代码了~ 

继续阅读