Android lets own app capture processing system keys home key and Power key

Source: Internet
Author: User

The home Key,power Key in Android responds by default to the framework of the system, what if you want your app to capture a response?


Home Key relatively simple, does not involve the system source code modification:

1. Add the following line to the OnCreate method of the main Activity in this app:

<span style= "FONT-SIZE:14PX;" >getwindow (). SetFlags (flag_homekey_dispatched, flag_homekey_dispatched);</span>

2. Override this main Activity's OnKeyDown () method to implement the Home key Processing section, andultimately

return true;


Power Key a little more trouble, to modify the source code:

1: Modify the Interceptkeybeforequeueing method of Phonewindowmanager.java, add the following code after reference line

Case Keyevent.keycode_power: {//reference lineresult &= ~action_pass_to_user;//reference line//add Beginactivitymanager Activitymanager = (activitymanager) getsystemservice (Context.activity_service); Try{if ( Activitymanager.getrunningtasks (1). Get (0). Topactivity.getpackagename (). Equals ("Your_special_package_name")) {// Please reset the special package Name.result |= Action_pass_to_user; XLOG.D (TAG, "Detect power key in special package, pass to User!")}} catch (NullPointerException e) {xlog.d (TAG, "Ingore a nullpointerexception.."); Add End


2: Also within this branch, find the Else statement segment and find the following location to join
if (Interceptpowerkeyup (canceled | | mpendingpowerkeyupcanceled)) {//reference line if (! ( (result& action_pass_to_user) = = Action_pass_to_user)) {//join line result = (Result & ~action_wake_up) | Action_go_to_sleep; Legacy Code}}//join line

Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui

My github: Zhou Mushi's GitHub Https://github.com/zhoumushui


Android lets own app capture processing system keys home key and Power key

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.