dialog,activity 屏蔽Home键详解

相信在Android应用上,很多时候逻辑是需要屏蔽Home键的,但这个用户体验是否需要,就看各位的需求了。
一般的方法屏蔽Home键,大家一定看过不少文章了。我总结一下,先说一下一般情况下Activity的屏蔽按键和Home键吧。
[b][color=red]屏蔽其他键,重写onKeyDown[/color][/b]
@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键等系统级按键是有一定的处理的。
[quote]看看源码是怎样处理的 \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;
}
}
}
[/quote]
通过源码,我们不难发现两个的参数 WindowManager.LayoutParams.TYPE_KEYGUARD和
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
[color=red][b]借鉴于此,重写onAttachedToWindow,以实现屏蔽Home键[/b][/color]
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键。至于其中到底哪一代码导致样式改变呢,那就以后再去看源代码了~

希望大家看到这个文章能觉得有用,再次谢谢已阅者! :wink:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值