屏蔽其他鍵,重寫onKeyDown
Java代碼
- @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
Java代碼
- // 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鍵
Java代碼
- public void onAttachedToWindow() {
- this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
- super.onAttachedToWindow();
- }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 華麗的分界線- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
輪到dialog了,如果在Activity彈出dialog,在Activity設置以上2個方法是沒辦法屏蔽的。
其實,原理是一樣的,只是地方不一樣而已。
Java代碼
- 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;
- }
- });
這樣運行後,出錯如下:
Error代碼
- 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:)其實,在源碼裡是這樣調用的。
Java代碼
- 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就可以了,如:
Java代碼
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
- }
- });
但前提是重載Activity的onAttachedToWindow(),哪怕只是一個空實現,然後返回父類方法。
Java代碼
- @Override
- public void onAttachedToWindow() {
- super.onAttachedToWindow();
- }
5:)其實它們,都是常用的~
Java代碼
- 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;
- }