Android 我可以使用服务来处理触摸事件吗?
我是一个新的android开发者。我想使用一个服务来处理触摸事件,这里是代码,只有doTouch()可以工作,我的意思是只显示“触摸”。如果我想显示“向下”、“指针向下”、“开始”,我该怎么做?有什么帮助吗?提前谢谢! logcat和控制台没有显示任何错误Android 我可以使用服务来处理触摸事件吗?,android,service,android-service,multi-touch,android-gesture,Android,Service,Android Service,Multi Touch,Android Gesture,我是一个新的android开发者。我想使用一个服务来处理触摸事件,这里是代码,只有doTouch()可以工作,我的意思是只显示“触摸”。如果我想显示“向下”、“指针向下”、“开始”,我该怎么做?有什么帮助吗?提前谢谢! logcat和控制台没有显示任何错误 public class MainService extends Service { private ViewService mViewService = null; @Override public IBin
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;
}
}
}
事实上,我并没有完美地解决这个问题。OnTouchListener仅用于单触。
[因此,我提出一个新问题][1]
http://stackoverflow.com/review/suggested-edits/5694854
公共布尔onTouch(视图v,运动事件e){
//TODO自动生成的方法存根
开关(e.getAction()&MotionEvent.ACTION_掩码){
case MotionEvent.ACTION_外部:
日志d(标签“触摸”);
MainService.this.doTouch();
//工作
//中断;
case MotionEvent.ACTION\u DOWN:
日志d(标记“向下”);
//工作
//中断;
case MotionEvent.ACTION\u指针\u向下:
Log.d(标记“指针向下”);
对于(int i=0;i=2){
//无功,指针计数==1。
//我想我应该使用onTouchEvent(),
//但我不知道如何用正确的方式书写。
}
}
}
您能更具体一点吗?您是否收到任何错误?请阅读如何在此服务中实现单触到多触?这是link[link],您似乎已经解决了问题。请修改问题内容,并将您的解决方案作为答案发布,这样我们就可以投票,也可以为下一个遇到这个问题的人投票。事实上,我并没有完美地解决这个问题。OnTouchListener仅用于单触。所以我问了一个新问题。这里是link[link]
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.
}
}
}