我希望使用服务代替活动来实现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));