主要用到方法:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
但是我試過發現在2.3.6 & 4.0 以上的機型,還是無法正常屏蔽~~~~
下面是使用的情況
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.e(TAG, "KEYCODE = "+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
Log.e(TAG, "HOME KEYCODE");
Toast.makeText(this, "Click HOME KEYCODE", Toast.LENGTH_SHORT).show();
// android.os.Process.killProcess(android.os.Process.myPid());
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
break;
case KeyEvent.KEYCODE_POWER:
Log.e(TAG, "POWER KEYCODE");
Toast.makeText(this, "Click POWER KEYCODE", Toast.LENGTH_SHORT).show();
}
return false;
}
大家会发现,这里屏蔽Home键是捕捉不到的,因为大家的权限一般是User所以是无效的。
而其实Android处理Home键等系统级按键是有一定的处理的。
看看源码是怎样处理的 \frameworks\policies\base\phone\com\Android\internal\policy\impl\PhoneWindowManager.java #1092
Java代码
1.// First we always handle the home key here, so applications
2.// can never break it, although if keyguard is on, we do let
3.// it handle it, because that gives us the correct 5 second
4.// timeout.
5.if (code == KeyEvent.KEYCODE_HOME) {
6.
7. // If a system window has focus, then it doesn't make sense
8. // right now to interact with applications.
9. WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
10. if (attrs != null) {
11. final int type = attrs.type;
12. if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
13. || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
14. // the "app" is keyguard, so give it the key
15. return false;
16. }
17. final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
18. for (int i=0; i
20. // don't do anything, but also don't pass it to the app
21. return true;
22. }
23. }
24. }
通过源码,我们不难发现两个的参数 WindowManager.LayoutParams.TYPE_KEYGUARD和
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
借鉴于此,重写onAttachedToWindow,以实现屏蔽Home键
Java代码
1.public void onAttachedToWindow() {
2. this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
3. super.onAttachedToWindow();
4.}
轮到dialog了,如果在Activity弹出dialog,在Activity设置以上2个方法是没办法屏蔽的。
其实,原理是一样的,只是地方不一样而已。
Java代码
1.final Dialog dialog = new Dialog(this);
2.dialog.setContentView(R.layout.mydailog);
3.dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
4.dialog.show();
5.
6.dialog.setOnKeyListener(new Android.content.DialogInterface.OnKeyListener(){
7. @Override
8. public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
9. switch (keyCode) {
10. case KeyEvent.KEYCODE_BACK:
11. Log.i(TAG,"KEYCODE_BACK");
12. return true;
13. }
14. return false;
15. }
16.});
这样运行后,出错如下:
Error代码
1.10-18 13:27:06.380: ERROR/AndroidRuntime(4684): Caused by: android.view.WindowManager$BadTokenException: Unable to add windowandroid.view.ViewRoot$W@2b046d68 -- permission denied for this window type
其实,只需要把dialog.getWindow().setType的位置放在show后面就可以了
正确代码
1.dialog.show();
2.dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
这么,就完成了Back键的屏蔽 和Home键盘的屏蔽了!
总结:
1:)在以上用WindowManager.LayoutParams.TYPE_KEYGUARD的地方改用
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG 效果一样。至于两者的具体差别,得以后再研究研究。
2:)其实,在源码里是这样调用的。
Java代码
1.final AlertDialog dialog = new AlertDialog.Builder(mContext)
2. .setTitle(null)
3. .setMessage(message)
4. .setNeutralButton(R.string.ok, null)
5. .create();
6.dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
7.dialog.show();
但我们如果这样调用就会出现之前的那个error:permission denied for this window type 这就显而易见了吧~~
3:)ProgressDialog 默认屏蔽 Back键,Dialog,AlertDialog则需setOnKeyListener
4:)其实屏蔽Home键,在页面的某个地方,例如一个Button的onClick里,去设置setType就可以了,如:
Java代码
1.button.setOnClickListener(new View.OnClickListener() {
2. @Override
3. public void onClick(View v) {
4. getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
5. }
6.});