歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android下Dialog及Activity屏蔽Home鍵詳解

Android下Dialog及Activity屏蔽Home鍵詳解

日期:2017/3/1 11:09:24   编辑:Linux編程
屏蔽其他鍵,重寫onKeyDown
Java代碼
  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent event) {
  3. Log.i(TAG,"keycode="+keyCode + " isBan="+isBan);
  4. switch (keyCode) {
  5. case KeyEvent.KEYCODE_BACK:
  6. Log.i(TAG,"KEYCODE_BACK");
  7. return true;
  8. }
  9. return super.onKeyDown(keyCode, event);
  10. }

大家會發現,這裡屏蔽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. // If a system window has focus, then it doesn't make sense
  7. // right now to interact with applications.
  8. WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
  9. if (attrs != null) {
  10. final int type = attrs.type;
  11. if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
  12. || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
  13. // the "app" is keyguard, so give it the key
  14. return false;
  15. }
  16. final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
  17. for (int i=0; i<typeCount; i++) {
  18. if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
  19. // don't do anything, but also don't pass it to the app
  20. return true;
  21. }
  22. }
  23. }

通過源碼,我們不難發現兩個的參數 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. dialog.setOnKeyListener(new android.content.DialogInterface.OnKeyListener(){
  6. @Override
  7. public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
  8. switch (keyCode) {
  9. case KeyEvent.KEYCODE_BACK:
  10. Log.i(TAG,"KEYCODE_BACK");
  11. return true;
  12. }
  13. return false;
  14. }
  15. });

這樣運行後,出錯如下:
Error代碼
  1. 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後面就可以了
正確代碼
  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. });

但前提是重載Activity的onAttachedToWindow(),哪怕只是一個空實現,然後返回父類方法。
Java代碼
  1. @Override
  2. public void onAttachedToWindow() {
  3. super.onAttachedToWindow();
  4. }

5:)其實它們,都是常用的~ Java代碼
  1. switch (keyCode) {
  2. case KeyEvent.KEYCODE_HOME:
  3. Log.i(TAG,"KEYCODE_HOME");
  4. return true;
  5. case KeyEvent.KEYCODE_BACK:
  6. Log.i(TAG,"KEYCODE_BACK");
  7. return true;
  8. case KeyEvent.KEYCODE_CALL:
  9. Log.i(TAG,"KEYCODE_CALL");
  10. return true;
  11. case KeyEvent.KEYCODE_SYM:
  12. Log.i(TAG,"KEYCODE_SYM");
  13. return true;
  14. case KeyEvent.KEYCODE_VOLUME_DOWN:
  15. Log.i(TAG,"KEYCODE_VOLUME_DOWN");
  16. return true;
  17. case KeyEvent.KEYCODE_VOLUME_UP:
  18. Log.i(TAG,"KEYCODE_VOLUME_UP");
  19. return true;
  20. case KeyEvent.KEYCODE_STAR:
  21. Log.i(TAG,"KEYCODE_STAR");
  22. return true;
  23. }
Copyright © Linux教程網 All Rights Reserved