本文介绍了如何使一个服务监听onTouchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下code

  public void onCreate() {
        super.onCreate();
    //    Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
        mView = new HUDView(this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //return super.onTouchEvent(event);

         final int action = ev.getAction();
         //   switch (action & MotionEvent.ACTION_MASK) {
         //   case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                Points p = new Points();
                p.setPoints(x,y);
                list.add(p);


        return true;
    }

这code是完美的Andr​​oid版本的工作,但2.3是onTouchEvent未版本4.0和4.1得到调用。可能是什么问题。

This code is working perfectly on Android ver 2.3 but onTouchEvent is not getting invoked in ver 4.0 and 4.1. What could be the problem

我想听听来自服务水龙头,服务上安装有活动。该活动照常使用,但服务保持拍摄水龙头。使用TYPE_SYSTEM_OVERLAY当其作品在Android 2.3,但是,4.1非常精细的onTouchEvent不调用。当使用TYPE_SYSTEM_ALERT onTouchEvent被调用,但在触摸事件不传递到活性,并且它变得无法响应。

I am trying to listen taps from a service, service is attached with a Activity. The activity is used as usual but service keep recording taps. when using TYPE_SYSTEM_OVERLAY its works very fine on Android 2.3 but on 4.1 the onTouchEvent is not invoked. When using TYPE_SYSTEM_ALERT onTouchEvent is invoked but the the touch event is not passed to the activity, and it becomes unresponsive.

推荐答案

试试这个:

findViewById(R.id.someView).setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent ev) {
        final int action = ev.getAction();
        final float x = ev.getX();
        final float y = ev.getY();
        Points p = new Points();
        p.setPoints(x,y);
        list.add(p);

        return true;        
    }
});

这篇关于如何使一个服务监听onTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:54