Android中屏蔽返回鍵,HOME鍵以及類比HOME鍵返回效果的方法

來源:互聯網
上載者:User

在Android開發中,有時需要屏蔽一些實體的按鍵,如HOME鍵,返回鍵等實體的按鍵,實現的方法如下。

1. 屏蔽實體按鍵

屏蔽實體按鍵(HOME鍵有些特別,需要單獨去分析)

switch (keyCode) {case KeyEvent.KEYCODE_HOME:return true;case KeyEvent.KEYCODE_BACK:return true;case KeyEvent.KEYCODE_CALL:return true;case KeyEvent.KEYCODE_SYM:return true;case KeyEvent.KEYCODE_VOLUME_DOWN:return true;case KeyEvent.KEYCODE_VOLUME_UP:return true;case KeyEvent.KEYCODE_STAR:return true;default:break;}


屏蔽HOME鍵

@Overridepublic void onAttachedToWindow() {// TODO Auto-generated method stubthis.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);super.onAttachedToWindow();}


2. 在有些應用中我們希望退出這個介面之後程式仍舊在運行,說白了就是類比HOME鍵的功能,其實現代碼如下

View.OnClickListener btnHomeListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(Intent.ACTION_MAIN);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 注意intent.addCategory(Intent.CATEGORY_HOME);startActivity(intent);// finish();}};


 特別注意 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 這句,一定要加上,因為Activity 的 launchMode 預設是以 standard 執行的,如果不添加這個標記則 會建立新的Activity 並放在與當前Activity相同的Task中。

相關文章

聯繫我們

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