Home button to get listening, Android 4.0 can not be obtained in the OnKeyDown method. What to do.

Source: Internet
Author: User
Get the home button press message under Android

Under Android, it is not possible to intercept the home key message through events such as onkeydown, for reasons that have been explicitly stated in the Android documentation

Translation will not do, in short, the app can not intercept the Home button event (there was a high man in the 2.3 below the system, using the method to intercept home, but after 4.0 has been invalidated, so do not mention)

Then, how to get the home button to press the message, the method or some, in this need to be explained, because of the particularity of the home key, its logic within the framework is processed, so can not intercept/block home key, but only to get its message, but after the message is received, If you want to get back to the background of the app to start up again, it is not impossible (the interface will flash)

The following code captures the press message to the home key:

At the same time, we also need to register this receiver in the program in order to make it effective


In this way, in the above two todo places, you can add their own code, it should be noted that after 4.0, the home key may have two reason, the original home and display the most recent application, in receiver, by judging the reason string, can be processed separately.

Home button to get listening, Android 4.0 can not be obtained in the OnKeyDown method. What to do.

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.