android launcher开发(4) Home键与launcher

您所在的位置:网站首页 安卓按住返回键和概览 android launcher开发(4) Home键与launcher

android launcher开发(4) Home键与launcher

2023-09-10 10:10| 来源: 网络整理| 查看: 265

Home键

现在我有一个红米手机,点击home键,退出当前的应用程序,如果我长按,那么他会显示最近使用的应用。点击home键回到launcher界面,那么是不是说launcher监听到了home的点击事件。 答案是否定的,home的点击事件无法被应用程序所捕获,launcher也不例外。

Home点击事件处理

总从3.0之后,安卓实现了导航栏,SystemUi,他包含三个按钮,后退,home,最近访问按钮(那个小方块);home的keycode是3; 当我们按下home键的时候,InputManager调用injectInputevent方法把这个事件传递过去,当然参数里面有keycode,标识这是个home事件。 导航栏发出home事件,然后来到窗口管理器,窗口管理器会调用launch HomeFromHotKey处理点击事件,调用handleLongPressOnhome处理长按事件。 通过代码我们可以看出,处理点击事件会调用一个方法,启动一个homeintent的activity。那么,什么事homeintent?

homeintent=new Intent(Intent.ACTION_MAIN,null); homeintent.addCategory(Intent.CATEGORY_HOME); homeintent.addFlags(....NEW_TASK);

再看看第一节中的配置文件,显然这说的就是Launcher。

对于长按时间的处理,不同的厂商进行不同的处理。当点击home键之后,重新启动launcher,调用onNewIntentI()方法。

onNewIntentI() @Override protected void onNewIntent(Intent intent) { long startTime = 0; if (DEBUG_RESUME_TIME) { startTime = System.currentTimeMillis(); } super.onNewIntent(intent); // Close the menu // 获得打开的文件夹 Folder openFolder = mWorkspace.getOpenFolder(); boolean alreadyOnHome = mHasFocus && ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction()); if (isActionMain) { // also will cancel mWaitingForResult. closeSystemDialogs(); //关闭所有系统面案,取消一些等待操作 if (mWorkspace == null) { // Can be cases where mWorkspace is null, this prevents a NPE return; } // In all these cases, only animate if we're already on home // 取消组件的大小尺寸调整 mWorkspace.exitWidgetResizeMode(); //关闭文件夹 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 the apps view if (!alreadyOnHome && mAppsView != null) { mAppsView.scrollToTop(); } // Reset the widgets view if (!alreadyOnHome && mWidgetsView != null) { mWidgetsView.scrollToTop(); } if (mLauncherCallbacks != null) { mLauncherCallbacks.onHomeIntent(); } } if (mLauncherCallbacks != null) { mLauncherCallbacks.onNewIntent(intent); } // Defer moving to the default screen until after we callback to the LauncherCallbacks // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage // animation. /* * 是否返回到默认桌面页面 * 参数 launcher处于前台 launcher处于显示状态 没有手势事件 没有打开的文件夹 是否要返回 * */ if (isActionMain) { boolean moveToDefaultScreen = mLauncherCallbacks != null ? mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true; if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() && openFolder == null && moveToDefaultScreen) { mWorkspace.post(new Runnable() { @Override public void run() { mWorkspace.moveToDefaultScreen(true); } }); } } if (DEBUG_RESUME_TIME) { Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime)); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3