监听广播
在我们每次点击Home按键时系统会发出action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的广播,用于关闭系统Dialog,此广播可以来监听Home按键。而我们的Launcher也是这样做的。在Launcher的onCreate中:
private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { closeSystemDialogs(); Log.i("Launcher","closeSystemDialogs"); } }
onUserLeaveHint方法
/** * 此方法当用户点击home键或者menu键离开launcher应用时调用 */ protected void onUserLeaveHint() { super.onUserLeaveHint(); sPausedFromUserAction = true; Log.i(TAG,"onUserLeaveHint"); }
导航栏home按键触发intent
mHomeIntent = new Intent(Intent.ACTION_MAIN, null); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
@Override protected void onNewIntent(Intent intent) { Log.i("Launcher","onNewIntent"); long startTime = 0; if (DEBUG_RESUME_TIME) { startTime = System.currentTimeMillis(); } super.onNewIntent(intent); // Close the menu if (Intent.ACTION_MAIN.equals(intent.getAction())) { // also will cancel mWaitingForResult. closeSystemDialogs(); final boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); Runnable processIntent = new Runnable() { public void run() { if (mWorkspace == null) { // Can be cases where mWorkspace is null, this prevents a NPE return; } Folder openFolder = mWorkspace.getOpenFolder(); // In all these cases, only animate if we're already on home mWorkspace.exitWidgetResizeMode(); if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() && openFolder == null) { mWorkspace.moveToDefaultScreen(true); } closeFolder(); exitSpringLoadedDragMode(); // If we are already on home, then just animate back to the workspace, // otherwise, just wait until onResume to set the state back to Workspace if (alreadyOnHome) { showWorkspace(true); } else { mOnResumeState = State.WORKSPACE; } final View v = getWindow().peekDecorView(); if (v != null && v.getWindowToken() != null) { InputMethodManager imm = (InputMethodManager)getSystemService( INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } // Reset AllApps to its initial state if (!alreadyOnHome && mAppsCustomizeTabHost != null) { mAppsCustomizeTabHost.reset(); } } }; if (alreadyOnHome && !mWorkspace.hasWindowFocus()) { // Delay processing of the intent to allow the status bar animation to finish // first in order to avoid janky animations. mWorkspace.postDelayed(processIntent, 350); } else { // Process the intent immediately. processIntent.run(); } } if (DEBUG_RESUME_TIME) { Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime)); } }
文章来源: 【Launcher小知识点】home按键监听