publicclassHomeKeyReceiverextendsBroadcastReceiver { privatestaticfinalStringTAG= HomeKeyReceiver.class.getSimpleName(); /* extra from home key broadcase receiver */ privatestaticfinalStringSYSTEM_DIALOG_REASON_EXTRA="reason"; /* press home key to go back to home */ privatestaticfinalStringSYSTEM_DIALOG_REASON_HOME_KEY="homekey"; /* long press home key to show recent apps */ privatestaticfinalStringSYSTEM_DIALOG_REASON_RECENT_APPS="recentapps"; /* (long) press home key to show assistant */ privatestaticfinalStringSYSTEM_DIALOG_REASON_ASSIST="assist"; /* (long) press home key to lock */ privatestaticfinalStringSYSTEM_DIALOG_REASON_LOCK="lock"; private Context mContext; /* listeners used in this broadcast receiver */ private OnHomeKeyListener mOnHomeKeyListener; private OnRecentAppListener mOnRecentAppListener; private OnAssistListener mOnAssistListener; private OnLockListener mOnLockListener; publicHomeKeyReceiver(Context context) { mContext = context; } /** * Interface definition for a callback to be invoked when the reason is "homekey". */ publicinterfaceOnHomeKeyListener { voidonKeypressed(); } /** * Interface definition for a callback to be invoked when the reason is "recentapps". */ publicinterfaceOnRecentAppListener { voidonKeypressed(); } /** * Interface definition for a callback to be invoked when the reason is "assist". */ publicinterfaceOnAssistListener { voidonKeypressed(); } /** * Interface definition for a callback to be invoked when the reason is "lock". */ publicinterfaceOnLockListener { voidonKeypressed(); } @Override publicvoidonReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { Stringreason= intent.getStringExtra(SYSTEM_DIALOG_REASON_EXTRA); if (TextUtils.isEmpty(reason)) { return; } switch (reason) { case SYSTEM_DIALOG_REASON_HOME_KEY: if (mOnHomeKeyListener != null) { mOnHomeKeyListener.onKeypressed(); } break; case SYSTEM_DIALOG_REASON_RECENT_APPS: if (mOnRecentAppListener != null) { mOnRecentAppListener.onKeypressed(); } break; case SYSTEM_DIALOG_REASON_ASSIST: if (mOnAssistListener != null) { mOnAssistListener.onKeypressed(); } break; case SYSTEM_DIALOG_REASON_LOCK: if (mOnLockListener != null) { mOnLockListener.onKeypressed(); } break;
default: Log.d(TAG, "other reason: " + reason); break; } } } /** * Register a callback to be invoked when system dialog reason is "homekey". */ publicvoidsetOnHomeKeyListener(OnHomeKeyListener l) { mOnHomeKeyListener = l; } /** * Register a callback to be invoked when system dialog reason is "recentapps". */ publicvoidsetOnRecentAppListener(OnRecentAppListener l) { mOnRecentAppListener = l; } /** * Register a callback to be invoked when system dialog reason is "assist". */ publicvoidsetOnAssistListener(OnAssistListener l) { mOnAssistListener = l; } /** * Register a callback to be invoked when system dialog reason is "lock". */ publicvoidsetOnLockListener(OnLockListener l) { mOnLockListener = l; }
/** * Register the home key boradcasr receiver. */ publicvoidregister() { IntentFilterhomeFilter=newIntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); mContext.registerReceiver(this, homeFilter); } /** * Unregister the home key boradcasr receiver. */ publicvoidunregister() { mContext.unregisterReceiver(this); }
由于 home 键在不同手机上,长 / 短按有不同的效果,代码列举出常用的几种,其中 homekey 表示短按 home 键退到桌面。