In the game often need to listen to the Android home button, when the home button under pressure, often need to do some state preservation, sound effects, such as the operation, then how to do to hear the home button? We know that home is a system key and cannot be handled by onkey these functions in the app. To properly handle the home button, you first need to look at the home button under pressure, what the system did.
- Home key in the ins and outs
- Receive the Home key broadcast event in the app
First, customize a broadcastreceiver:
classHomekeyeventbroadcastreceiverextendsBroadcastreceiver {Static FinalString System_reason = "REASON"; Static FinalString System_home_key = "HomeKey";//Home Key Static FinalString System_recent_apps = "Recentapps";//Long Home key@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (intent.action_close_system_dialogs)) {String reason=Intent.getstringextra (System_reason); if(Reason! =NULL) { if(Reason.equals (System_home_key)) {//Home key processing point } Else if(Reason.equals (System_recent_apps)) {//Long Home key processing point } } } } }
Second, the generated and registered
New homekeyeventbroadcastreceiver (); New Intentfilter (intent.action_close_system_dialogs));
This way, we can do the home button processing in the app!
I'm the dividing line of the king of the Land Tiger.
Reference: http://blog.csdn.net/imyfriend/article/details/8293399