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.