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