我是一名新的Android开发人员。我想使用一个服务来处理触摸事件,这里是代码,只有doTouch()工作,我的意思是show"触及"如果我想要显示" DOWN"," POINTER_DOWN"," BEGIN",我该怎么做?任何帮助?提前致谢! logcat和控制台没有显示任何错误。
public class MainService extends Service {
private ViewService mViewService = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
startForeground(1, new Notification());
mViewService = ViewService.getInstance(this);
mViewService.addView(ViewService.TOUCH_VIEW);
mViewService.getView(ViewService.TOUCH_VIEW).setOnTouchListener(new TouchListener());
}
//do touch event
public void doTouch(){
Toast.makeText(this, "touched", Toast.LENGTH_SHORT).show();
}
class TouchListener implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction() == MotionEvent.ACTION_OUTSIDE){
MainService.this.doTouch();
//Toast.makeText(getBaseContext(), "TOUED", Toast.LENGTH_SHORT).show();
}
/*else if(e.getAction() == MotionEvent.ACTION_DOWN){
MainService.this.doTouch();
}*/
/*if(e.getPointerCount() >= 3){
MainService.this.doTouch();
}*/
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(getBaseContext(), "DOWN", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_POINTER_DOWN:
if(e.getPointerCount() >= 3){
Toast.makeText(getBaseContext(), "POINTER_DOWN", Toast.LENGTH_SHORT).show();
}
case MotionEvent.ACTION_MOVE:
Toast.makeText(getBaseContext(), "BEGIN", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
}
}
答案 0 :(得分:0)
Actually,I don't solved the problem perfect. OnTouchListener use for single touch only.
[so I ask an new question][1]
http://stackoverflow.com/review/suggested-edits/5694854
public boolean onTouch(View v, MotionEvent e){
// TODO Auto-generated method stub
switch (e.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_OUTSIDE:
Log.d(TAG, "touched");
MainService.this.doTouch();
//work
//break;
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down");
//work
//break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG, "pointer down");
for(int i=0;i<e.getPointerCount();i++){
downX[i] = e.getX(i);
downY[i] = e.getY(i);
//work
}
case MotionEvent.ACTION_MOVE:
int pointerCount=e.getPointerCount();
Log.d(TAG, "move");
Log.d(TAG, "c=" + pointerCount);
if(pointerCount >= 2){
//no work, pointerCount == 1 forever.
//I think I should use onTouchEvent(),
//but I dont know how to write on right way.
}
}
}