How to shield the return key, Home key, and simulate the return effect of the home key in Android

Source: Internet
Author: User

In Android development, you sometimes need to block the buttons of some entities, such as the Home key and the Return key. The implementation method is as follows.

1. Mask entity buttons

Mask entity buttons (the home key is special and needs to be analyzed separately)

 
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 ;}


Shield The Home Key

 
@ Overridepublic void onattachedtowindow () {// todo auto-generated method stubthis. getwindow (). settype (windowmanager. layoutparams. type_keyguard); Super. onattachedtowindow ();}


2. In some applications, we want to exit the interfaceProgramIt is still running. To put it bluntly, it is to simulate the Home Key Function and Its ImplementationCodeAs follows:

 
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); // pay attention to intent. addcategory (intent. category_home); startactivity (intent); // finish ();}};


Pay special attention to intent. setflags (intent. flag_activity_new_task); this sentence must be added because the launchmode of the activity is executed in standard by default, if this tag is not added, a new activity is created and placed in the same task as the current activity.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.