android如何屏蔽home back power按鍵(現儲存以後很可能會需要的)

來源:互聯網
上載者:User

一般來說我們會在某些時候對home back和power鍵進行屏蔽,比如說原廠模式
  這個時候,我們可以選擇修改WindowsManagerService.java這個類,在相關的地方進行下屏蔽,這裡需要針對當前啟動並執行是某個特殊應用的時候才給予屏蔽。
  如果說是對home鍵進行屏蔽,我們可以在應用的代碼中加入語句:
public void onAttachedToWindow() {
            // TODO Auto-generated method stub
            //注意是TYPE_KEYGUARD_DIALOG而不是TYPE_KEYGUARD,可以屏蔽home鍵
            this.getWindow().setType(
                            WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            super.onAttachedToWindow();

    }即可

android 屏蔽按鍵

  1. UiModeManager uiModeManager = (UiModeManager )getSystemService(Context.UI_MODE_SERVICE);  
  2.           
  3.         if(uiModeManager.getCurrentModeType()==UiModeManager.DISABLE_CAR_MODE_GO_HOME){  
  4.             uiModeManager.enableCarMode(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME);  
  5.             flag = true;  
  6.             return;  
  7.         } 

public boolean dispatchKeyEvent (KeyEvent event)

Since: API Level 1
Called to process key events. You can override this to intercept all key events before they are dispatched to the window. Be sure to call this implementation for key events that should be handled normally.
Parameters
event

  1. @Override
  2. public boolean dispatchKeyEvent(KeyEvent event)
  3. {
  4.         switch (event.getKeyCode()){
  5.                 case KeyEvent.KEYCODE_VOLUME_UP:   //音量鍵+
  6.                 case KeyEvent.KEYCODE_VOLUME_DOWN:   //音量鍵-
  7.                 case KeyEvent.KEYCODE_CAMERA:  //拍照鍵
  8.                 case KeyEvent.KEYCODE_HOME:   //拍HOME鍵
  9.                         return true; //這些標記為處理過,則不在往內部傳遞
  10.                 default:
  11.             break;
  12.     }
  13.     return super.dispatchKeyEvent(event);
  14. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.