修改Launcher源码之快速启动

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

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

  2、修改Launcher源码,只要改一处,修改Workspace类的onInterceptTouchEvent方法

 

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;
    }

 

Workspace相当于你看到的一个桌面,触屏事件在到达onTouchEvent之前会先执行onInterceptTouchEvent方法。

如果你想通过在桌面空白处用触屏事件来启动你的应用的话,可以用此方法,不过用户不一定接受你的定制Launcher。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值