android launcher开发(4) Home键与launcher

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));
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值