Android uses broadcast listening to press the HOME and RECENT_APPS keys

Source: Internet
Author: User

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>

 

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.