addison_lin 发表于 2012-1-10 09:57:15

修改Launcher源码之快速启动

最近在写一个快速拨号的App,应用很简单,主要要突出一个快速的特点,启动要快,最好能从Home Screen启动,当然第一个反应是在桌面上新建一个快捷方式,不过我决定用另一种特别的方式,即:当用户在Home Screen中上下触屏时,启动此应用,左右触屏是切换桌面的。废话不多说,看怎样实现:

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

2、修改Launcher源码,只要改一处,修改Workspace类的onInterceptTouchEvent方法
Workspace相当于你看到的一个桌面,触屏事件在到达onTouchEvent之前会先执行onInterceptTouchEvent方法。
如果你想通过在桌面空白处用触屏事件来启动你的应用的话,可以用此方法,不过用户不一定接受你的定制Launcher。
[代码] 代码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 + (int) ev.getX(),
                              mTempCell + (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;
    }


页: [1]
查看完整版本: 修改Launcher源码之快速启动