Android Home Key listener and androidhome listener

Source: Internet
Author: User

Android Home Key listener and androidhome listener
Android Back Home key listener

 

Back key listening

It is easier to listen for the Back key. It can be intercepted in multiple system callbacks, for example, inIn the following methods of activity, you can receive the event of the Back key press.:

@ Override public void onBackPressed () {// super. onBackPressed (); // comment out this line. The back key does not exit activity Log. I (LOG_TAG, "onBackPressed") ;}@ Override public boolean dispatchKeyEvent (KeyEvent event) {Log. I (LOG_TAG, "dispatchKeyEvent: keyCode --" + event. getKeyCode (); return super. dispatchKeyEvent (event) ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {Log. I (LOG_TAG, "onKeyDown: keyCode --" + keyCode); switch (keyCode) {case KeyEvent. KEYCODE_BACK: Log. I (LOG_TAG, "KeyEvent. KEYCODE_BACK "); break; case KeyEvent. KEYCODE_MENU: Log. I (LOG_TAG, "KeyEvent. KEYCODE_MENU "); break; case KeyEvent. KEYCODE_HOME: Log. I (LOG_TAG, "KeyEvent. KEYCODE_HOME "); // cannot receive break; case KeyEvent. KEYCODE_APP_SWITCH: Log. I (LOG_TAG, "KeyEvent. KEYCODE_APP_SWITCH "); // cannot receive break; default: break;} return super. onKeyDown (keyCode, event );}

 

Broadcast listening with the Home Key

It is not so easy to listen to the Home key, because the Home Key can leave the program in the background, so this event is directly distributed to the system, and the system will handle it after receiving it, the Home key event is not directly transmitted to the application. so in the above listening Back key code,The corresponding callback fails to receive the Home key event..

Refer to the post-blog link,Listening to the Home Key is mainly implemented by registering a broadcast receiver.Intercept the system action for closing the window. Then, based on the specific parameters in the Intent, analyze whether it is the Home key, the application switch key, or other function buttons.

The receiver implementation is as follows:

Package com. mengdd. hellohome; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. util. log; public class HomeWatcherReceiver extends BroadcastReceiver {private static final String LOG_TAG = "HomeReceiver"; private static final String SYSTEM_DIALOG_REASON_KEY = "reason"; private static final String Signature = "recentapps "; private static final String delimiter = "homekey"; private static final String SYSTEM_DIALOG_REASON_LOCK = "lock"; private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist"; @ Override public void onReceive (Context context Context, intent intent) {String action = intent. getAction (); Log. I (LOG_TAG, "onReceive: action:" + action); if (action. equals (Intent. ACTION_CLOSE_SYSTEM_DIALOGS) {// android. intent. action. CLOSE_SYSTEM_DIALOGS String reason = intent. getStringExtra (SYSTEM_DIALOG_REASON_KEY); Log. I (LOG_TAG, "reason:" + reason); if (SYSTEM_DIALOG_REASON_HOME_KEY.equals (reason) {// press the Home Key for short Log. I (LOG_TAG, "homekey");} else if (system_dialog_reason_recent_assist.equals (reason) {// long press the Home key or activity switch key Log. I (LOG_TAG, "long press home key or activity switch");} else if (SYSTEM_DIALOG_REASON_LOCK.equals (reason) {// lock screen Log. I (LOG_TAG, "lock");} else if (SYSTEM_DIALOG_REASON_ASSIST.equals (reason) {// samsung long press the Home key Log. I (LOG_TAG, "assist ");}}}}

Note that the buttons on different mobile phones are different, so you need to differentiate the different reasons.

 

Home Key listens for broadcast Registration

There are two methods for registering a broadcast receiver. One is static registration, which is written in manifest and the other is dynamic registration, that is, registration in Java code.

In the above example, the consumer that implements the listener with the Home key. The static registration is as follows:

        <receiver android:name="com.mengdd.hellohome.HomeWatcherReceiver" >            <intent-filter>                <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />            </intent-filter>        </receiver>

HoweverStatic registration does not work,OnReceive is not receivedCallback.

 

Dynamic Registration:

    private static HomeWatcherReceiver mHomeKeyReceiver = null;    private static void registerHomeKeyReceiver(Context context) {        Log.i(LOG_TAG, "registerHomeKeyReceiver");        mHomeKeyReceiver = new HomeWatcherReceiver();        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);        context.registerReceiver(mHomeKeyReceiver, homeFilter);    }    private static void unregisterHomeKeyReceiver(Context context) {        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");        if (null != mHomeKeyReceiver) {            context.unregisterReceiver(mHomeKeyReceiver);        }    }

  

In the onResume and onPause of the Activity, call:

    @Override    protected void onResume() {        super.onResume();        registerHomeKeyReceiver(this);    }    @Override    protected void onPause() {        unregisterHomeKeyReceiver(this);        super.onPause();    }

Of course, you can also register and log out at other appropriate times as needed.

 

References

Http://blog.csdn.net/way_ping_li/article/details/8953622

 


In Android, how does one listen to the Back and Home keys?

Inherit Activity
Override OnKeyDown ()
Print the key value.
It seems to be on different mobile phones. The corresponding key values are different.
Obtain the Back and Home key values.
Then match the key value. Write what you want to do.
Back can be written as follows:
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_BACK ){

}
Return super. onKeyDown (keyCode, event );
}

: The android system disables the HOME key solution. You need to be able to listen to this event and shield it from being compatible with all versions of 22-42.

Declare permissions:
<Uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW"/>
<Uses-permission android: name = "android. permission. INTERNAL_SYSTEM_WINDOW"/>

Declare functions and members in the activity class:

Runnable mDisableHomeKeyRunnable = new Runnable (){

@ Override
Public void run (){
DisableHomeKey ();

}
};

Handler mHandler = new Handler ();

Public void disableHomeKey ()
{
This. getWindow (). setType (WindowManager. LayoutParams. TYPE_SYSTEM_ALERT );
}

Call in onCreate:
MHandler. postDelayed (mDisableHomeKeyRunnable, 200 );

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.