`
phoenixfu
  • 浏览: 96304 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

dialog,activity 屏蔽Home键详解

阅读更多
   相信在Android应用上,很多时候逻辑是需要屏蔽Home键的,但这个用户体验是否需要,就看各位的需求了。
    一般的方法屏蔽Home键,大家一定看过不少文章了。我总结一下,先说一下一般情况下Activity的屏蔽按键和Home键吧。
屏蔽其他键,重写onKeyDown
@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
        // 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键
public void onAttachedToWindow() {
	this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	super.onAttachedToWindow();
}

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

轮到dialog了,如果在Activity弹出dialog,在Activity设置以上2个方法是没办法屏蔽的。
其实,原理是一样的,只是地方不一样而已。
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;
	}
}); 

这样运行后,出错如下:
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后面就可以了
dialog.show();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

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

总结:
1:)在以上用WindowManager.LayoutParams.TYPE_KEYGUARD的地方改用
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG 效果一样。至于两者的具体差别,得以后再研究研究。

2:)其实,在源码里是这样调用的。
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);
dialog.show();

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

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

4:)其实屏蔽Home键,在页面的某个地方,例如一个Button的onClick里,去设置setType就可以了,如:
button.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}
});

但前提是重载Activity的onAttachedToWindow(),哪怕只是一个空实现,然后返回父类方法。
	@Override  
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
	}

5:)其实它们,都是常用的~
switch (keyCode) {
	case KeyEvent.KEYCODE_HOME:
		Log.i(TAG,"KEYCODE_HOME");
		return true;
	case KeyEvent.KEYCODE_BACK:
		Log.i(TAG,"KEYCODE_BACK");
		return true;
	case KeyEvent.KEYCODE_CALL:
		Log.i(TAG,"KEYCODE_CALL");
		return true;
	case KeyEvent.KEYCODE_SYM:
		Log.i(TAG,"KEYCODE_SYM");
		return true;
	case KeyEvent.KEYCODE_VOLUME_DOWN:
		Log.i(TAG,"KEYCODE_VOLUME_DOWN");
		return true;
	case KeyEvent.KEYCODE_VOLUME_UP:
		Log.i(TAG,"KEYCODE_VOLUME_UP");
		return true;
	case KeyEvent.KEYCODE_STAR:
		Log.i(TAG,"KEYCODE_STAR");
		return true;
}


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

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

2011-10-20 更新如下:
总结1:)的问题,有答案了,时间问题我就简单写写吧:
    从功能上来说,是一样的,区别在样式。
    如果你喜欢用Theme.Dialog去把一个Activity装饰成一个Dialog去显示,你会发现。
android:theme="@android:style/Theme.Dialog"

背景是透明的。
如果在
setTheme(android.R.style.Theme_Dialog);

背景则是黑色的。
这是为什么呢?。。。我不知道。
治标不治本的方法来了!若你在Activity重写onAttachedToWindow
public void onAttachedToWindow() {
	this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
	super.onAttachedToWindow();
}

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

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

分享到:
评论
1 楼 被射下的眼泪 2012-02-18  
很不错,解析的很到位!

相关推荐

Global site tag (gtag.js) - Google Analytics