MainActivity is as follows:
Package cn. testhomekey; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. content. intentFilter;/*** Demo Description: * The HOME and RECENT_APPS keys of the broadcast listening device are pressed. ** reference: * http://blog.csdn.net/imyfriend/article/details/8293399 * Thank you very much */public class MainActivity extends Activity {private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver; @ Overrideprotected Void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Override protected void onStart () {super. onStart (); mHomeKeyBroadcastReceiver = new HomeKeyBroadcastReceiver (); IntentFilter intentFilter = new IntentFilter (Intent. ACTION_CLOSE_SYSTEM_DIALOGS); this. registerReceiver (mHomeKeyBroadcastReceiver, intentFilter) ;}@ Overrideprotected void onDestroy ( ) {If (mHomeKeyBroadcastReceiver! = Null) {this. unregisterReceiver (mHomeKeyBroadcastReceiver);} super. onDestroy ();}}
HomeKeyBroadcastReceiver is as follows:
Package cn. testhomekey; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; public class HomeKeyBroadcastReceiver extends BroadcastReceiver {private final String SYSTEM_REASON = "reason"; // The Home key private final String SYSTEM_HOME_KEY = "homekey "; // recently used application key private final String SYSTEM_RECENT_APPS = "recentapps"; @ Overridepublic void onReceive (Context conte Xt, Intent intent) {String action = intent. getAction (); if (action. equals (Intent. ACTION_CLOSE_SYSTEM_DIALOGS) {String systemReason = intent. getStringExtra (SYSTEM_REASON); if (systemReason! = Null) {if (systemReason. equals (SYSTEM_HOME_KEY) {System. out. println ("press the HOME Key");} else if (systemReason. equals (SYSTEM_RECENT_APPS) {System. out. println ("Press RECENT_APPS ");}}}}}
Manifest. xml is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. testhomekey "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion = "8"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = "cn. testhomekey. main Activity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <! -- Register broadcast --> <javaser android: name = "cn. testhomekey. HomeKeyBroadcastReceiver"> </javaser> </application> </manifest>
Main. xml is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Listen to the HOME and RECENT_APPS keys" android: layout_centerInParent = "true"/> </RelativeLayout>