使用Service的GestureDetector

时间:2012-03-15 13:18:34

标签: java android service

我希望使用服务代替活动来实现GestureDetector。代码适用于活动但不适用于服务。在Service我无法@Override onTouchEvent(MotionEvent e)方法 - 这是问题的原因吗?

public class GestureService extends Service implements OnGestureListener {

private GestureDetector gestureScanner;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    super.onCreate();       
    Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show();
    gestureScanner = new GestureDetector(this);
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(getBaseContext(),"onStartCommand", Toast.LENGTH_SHORT).show();
    return START_STICKY;

 }

public boolean onTouchEvent(MotionEvent me) {
    return gestureScanner.onTouchEvent(me);
}

@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(),"onDown", Toast.LENGTH_SHORT).show();
    return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
@Override
public boolean onSingleTapUp(MotionEvent e) {
    Toast.makeText(getBaseContext(),"onSingleTapUp", Toast.LENGTH_SHORT).show();
    // TODO Auto-generated method stub
    return false;
}

}

我使用startService(new Intent(this,GestureService.class));

在活动类中启动了该服务

1 个答案:

答案 0 :(得分:0)

我猜你想把它变成一个服务,以便你可以将它用于所有应用程序。但是从阅读this article开始,它实际上并不是它的工作方式:手势是特定于应用的。