Android開發_關於如何屏蔽Home鍵

來源:互聯網
上載者:User

標籤:

今天在遇到一個要屏蔽Home鍵的問題,研究一上午終於解決,方法記錄於下:

  

    在Android2.3版本以下重寫以下方法就能屏蔽Home鍵:

public void onAttachedToWindow() {      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);      super.onAttachedToWindow();}

    在Android4.0以上版本用onAttachedToWindow()方法會報錯,需要利用以下方法屏蔽:

public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; //需要自己定義標誌public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//關鍵代碼    setContentView(R.layout.main);}

    再重寫onKeyDown方法即可:

@Overridepublic boolean onKeyDown( int keyCode, KeyEvent event) {    if (keyCode == event. KEYCODE_HOME) {        return true;      } return super.onKeyDown(keyCode, event);}

在AndroidMainfest.xml需要加許可權:  
<uses-permission android:name = "android.permission.DISABLE_KEYGUARD"/><!-- 屏蔽HOME鍵需要的許可權 -->

在設定檔中,在你使用了Notification的activity中加一個屬性android: android:launchMode="singleInstance"

Android開發_關於如何屏蔽Home鍵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.