首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

批改Launcher源码之快速启动

2012-08-24 
修改Launcher源码之快速启动? 最近在写一个快速拨号的App,应用很简单,主要要突出一个快速的特点,启动要快,

修改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。

1 楼 yelinsen05 2011-04-13   开发环境是哪个版本的!如果做平台的话!可以改Default APP!以及Framework其实实现方式其实有很多!这个快速启动!我想强调的重点在于快速拨号在读取联系人信息大数据量的时候做优化! 2 楼 Lucky_Man 2011-04-13   谢谢楼上的评论,我改的是Android 2.1,联系人信息数据量一般不会太大吧,上面的文章只是介绍如何快速启动一个程序

热点排行