阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1225|回复: 0

[转帖] 修改Launcher源码之快速启动

[复制链接]
  • TA的每日心情
    开心
    2012-3-7 10:15
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2012-1-10 09:57:15 | 显示全部楼层 |阅读模式
    最近在写一个快速拨号的App,应用很简单,主要要突出一个快速的特点,启动要快,最好能从Home Screen启动,当然第一个反应是在桌面上新建一个快捷方式,不过我决定用另一种特别的方式,即:当用户在Home Screen中上下触屏时,启动此应用,左右触屏是切换桌面的。废话不多说,看怎样实现:

    1、编译运行Launcher源码,具体可参照网上相关介绍,本来打算附上修改后的源码,不过文件太大,需要的话可以找我,建议参考网络资料自己动手编译。

    2、修改Launcher源码,只要改一处,修改Workspace类的onInterceptTouchEvent方法
    Workspace相当于你看到的一个桌面,触屏事件在到达onTouchEvent之前会先执行onInterceptTouchEvent方法。
    如果你想通过在桌面空白处用触屏事件来启动你的应用的话,可以用此方法,不过用户不一定接受你的定制Launcher。
    [代码] [Java]代码public boolean onInterceptTouchEvent(MotionEvent ev) {
            /*****************省略相关代码***********/
            final float x = ev.getX();
            final float y = ev.getY();
            Log.d("Workspace", "enter onInterceptTouchEvent");
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    /**
                     * mIsBeingDragged == false, otherwise the shortcut would have caught it.
                     * Check
                     * whether the user has moved far enough from his original down touch.
                     */
                    /**
                     * Locally do absolute value. mLastMotionX is set to the y value
                     * of the down event.
                     */
                    final int xDiff = (int) Math.abs(x - mLastMotionX);
                    final int yDiff = (int) Math.abs(y - mLastMotionY);
                    final int touchSlop = mTouchSlop;
                    boolean xMoved = xDiff > touchSlop;
                    boolean yMoved = yDiff > touchSlop;
                   
                    if (xMoved || yMoved) {
                      
                        if (xMoved) {
                            // Scroll if the user moved far enough along the X axis
                            mTouchState = TOUCH_STATE_SCROLLING;
                            enableChildrenCache();
                        }
       /*********************************添加的代码****************************************/
                        if(yMoved) {            //纵向移动
                            Log.d("workspace", "在这启动你的应用程序!");
                           
                            Intent intent = new Intent();                  
                            //设置应用的包名,类名
                            intent.setClassName("com.tek.qd", "com.tek.qd.QuickDialer");
                            //启动应用
                            mContext.startActivity(intent);
                            mTouchState = TOUCH_STATE_SCROLLING;
                            enableChildrenCache();
                        }
      /*********************************代码结束****************************************/
                        // Either way, cancel any pending longpress
                        if (mAllowLongPress) {
                            mAllowLongPress = false;
                            // Try canceling the long press. It could also have been scheduled
                            // by a distant descendant, so use the mAllowLongPress flag to
                            //block
                            // everything
                            final View currentScreen = getChildAt(mCurrentScreen);
                            currentScreen.cancelLongPress();
                        }
                    }
                    break;
                case MotionEvent.ACTION_DOWN:
                    // Remember location of down touch
                    mLastMotionX = x;
                    mLastMotionY = y;
                    mAllowLongPress = true;
                   
                    /**
                     * If being flinged and user touches the screen, initiate drag;
                     * otherwise don't.  mScroller.isFinished should be false when
                     * being flinged.
                     */
                   
                    mTouchState = mScroller.isFinished() ?
                                   TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (mTouchState != TOUCH_STATE_SCROLLING) {
                        final CellLayout currentScreen = (CellLayout) getChildAt(
                                                          mCurrentScreen);
                        if (!currentScreen.lastDownOnOccupiedCell()) {
                            getLocationOnScreen(mTempCell);
                            // Send a tap to the wallpaper if the last down was on
                            //empty space
                            mWallpaperManager.sendWallpaperCommand(getWindowToken(),
                                    "android.wallpaper.tap",
                                    mTempCell[0] + (int) ev.getX(),
                                    mTempCell[1] + (int) ev.getY(), 0, null);
                        }
                    }
                   
                    // Release the drag
                    clearChildrenCache();
                    mTouchState = TOUCH_STATE_REST;
                    mAllowLongPress = false;
                    break;
            }
            /**
             * The only time we want to intercept motion events is if we are in the
             * drag mode.
             */
            return mTouchState != TOUCH_STATE_REST;
        }


    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表