繁体   English   中英

我可以使用一项服务来处理触摸事件吗?

[英]Can I use a service to handle touch events?

我是一名新的android开发人员。 我想使用服务来处理触摸事件,这是代码,仅doTouch()有效,我的意思是仅显示“ touched”。如果我想显示“ 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;
     }
  }
 }
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.
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM