如何在应用程序外部的屏幕上使用检测手指滑动

时间:2016-07-30 13:08:23

标签: android

在我的应用中,我想使用GestureEvents来检测用户完成的滑动,如下所示:

GestureDetector gd=new GestureDetector(this,this); //code is implemented in MainActivity of my app

@Override
public boolean onTouchEvent(MotionEvent event){
    return gd.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    float sensitivity =50;
    if(e1.getX()-e2.getX()>sensitivity){
        Toast.makeText(MainActivity.this,"left swipe",Toast.LENGTH_SHORT).show();
        return true;
    }
    else if(e2.getX()-e1.getX()>sensitivity){
        Toast.makeText(MainActivity.this,"right swipe",Toast.LENGTH_SHORT).show();
        return true;
    }
   else{
        return true;
    }
}

从上面的代码我可以了解用户在屏幕上向左或向右滑动的天气。但是当我的应用程序变为背景时,它检测到我的活动处于前景,但它没有检测到屏幕上的任何动作。我想要即使应用程序被破坏,它也可以在我的应用程序之外工作。我有可能这样做,请帮助我。

1 个答案:

答案 0 :(得分:0)

  

使用以下代码进行滑动检测,可能对您有帮助,在我的项目中完美运行,所以您可以试试这个..

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;
int SWIPE_THRESHOLD = 200;
int SWIPE_VELOCITY_THRESHOLD = 200;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new CustomGestureListenerClass());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class CustomGestureListenerClass extends SimpleOnGestureListener {


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        singleClicked(e);
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) {
        return super.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY);
    }


    @Override
    public boolean onFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) {

        boolean result = false;
        try {
            float diffY = endMotionEvent.getY() - startMotionEvent.getY();
            float diffX = endMotionEvent.getX() - startMotionEvent.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            }
            result = true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
    // if you want done some portion before call this method then write here
}

public void onSwipeLeft() {
    // if you want done some portion before call this method then write here
}

public void singleClicked(MotionEvent e) {
}
}

并使用以下代码配置活动

getWindow().getDecorView().setOnTouchListener(new OnSwipeTouchListener(this) {
});