Android uses the code to simulate the HOME key function. The android code home Key

Source: Internet
Author: User

Android uses the code to simulate the HOME key function. The android code home Key
Intent intent = newIntent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_HOME );
Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK | Intent. FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
Context object. startActivity (intent );

How does Android simulate the Home Key in its own application?

I first thought of the Override onKeyDown method. However, after debugging, I found that when the Home key is pressed, the system will not send messages to this system for processing. That is to say, simulating KEYCODE_HOME is ineffective, then I found the dispatchKeyEvent function on the Internet. The following is a function description: Called to process key events. you can override this to intercept all key events before they are dispatched to the window. be sure to call this implementation for key events that shocould be handled normally. however, the message generated by the Home button does not pass through this process. It seems that the message generated by the Home button is processed by the system. So I thought of starting from other aspects and finally found the following method: @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stub if (keyCode = KeyEvent. KEYCODE_BACK) {Intent intent = new Intent (Intent. ACTION_MAIN); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // pay attention to intent. addCategory (Intent. CATEGORY_HOME); this. startActivity (intent); return true;} return super. onKeyDown (keyCode, event);} pay special attention to inten T. 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.

How can I set the home key on an Android phone, for example, to start a software? My mobile phone can only jump to the system desktop to change

It seems that you are not talking about anything.

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.