The Home Key listens, shields, and simulates the Home Key.

Source: Internet
Author: User
 
/*** Press the Home Key to return the program to the background and run the * @ Param context */private void imitatepresshome (context) {intent = new intent (intent. action_main); intent. setflags (intent. flag_activity_new_task); intent. addcategory (intent. category_home); context. startactivity (intent);}/*** register the Home Key listener * @ Param context */private void registerhomereceiver (context) {context. registerreceiver (mhomekeyeventreceiver, new intentfilter (intent. action_close_system_dialogs);}/*** Home Key Processing */private broadcastreceiver mhomekeyeventreceiver = new broadcastreceiver () {string system_reason = "reason"; string system_home_key = "homekey "; string system_home_key_long = "recentapps"; @ override public void onreceive (context, intent) {string action = intent. getaction (); If (action. equals (intent. action_close_system_dialogs) {string reason = intent. getstringextra (system_reason); If (textutils. equals (reason, system_home_key) {// indicates that the home key is pressed, and the program goes to the background toast. maketext (context, "Home pressed", 1000 ). show ();} else if (textutils. equals (reason, system_home_key_long) {// means to press the Home Key to display the list of recently used programs toast. maketext (context, "home long pressed", 1000 ). show () ;}}};/*** Home Key mask */@ override public void onattachedtowindow () {// todo auto-generated method Stub this. getwindow (). settype (windowmanager. layoutparams. type_keyguard); super. onattachedtowindow ();}


After 4.0, the type of the window cannot be reset in both oncreate () and onattachedtowindow (). It is invalid to use this method to shield the Home Key!
If you run the code above 4.0, Java is reported. lang. illegalargumentexception: window type can not be changed after the window is added. as for how to shield it, there is no good solution on the Internet now. MTK has a solution, and other platforms will not work .... unless you modify the underlying framework
MTK solution:
Public static final int flag_homekey_dispatched = 0x80000000;
Run in oncreate
This. getwindow (). setflags (flag_homekey_dispatched, flag_homekey_dispatched );

The Home Key listens, shields, and simulates the Home Key.

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.