MotionEvent.obtain(...);没有在屏幕攻丝机上工作

时间:2015-05-18 02:39:52

标签: java android multithreading service android-view

这是我的第一个主题here的延续,关于如何使用代码来模仿屏幕触摸。

我想将这个功能合并到一个浮动的,屏幕上的聊天头上(就像facebook所做的那样)。所以基本上,当我点击聊天头时,这项服务将模仿一定次数并以一定的速率点击。 (这是在其他地方预定义的。)它还将模拟某个点x,y处的点击。 (这也是预定义的。)我希望服务能够点击后面的任何内容(在屏幕上)。

以下是我的完整代码,但它不起作用,也没有显示任何错误。 这有点长而且冗长,如果你没有多少时间,那么就跳到可运行的部分。我非常有信心在runnable部分之前没有任何错误。

public class TapService extends Service{

    private WindowManager windowManager;
    private ImageView chatHead;

    boolean mHasDoubleClicked = false;
    long lastPressTime;
    private Boolean _enable = true;

    int x = 0;
    int y = 0;

    int tapTimes;
    long delayInterval;

    @Override
    public IBinder onBind(Intent intent) {
        // Not Used
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //////////////Chathead Core Section
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.floating2);

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(chatHead, params);
        //////////Chathead Core Section

        getTapInformation();

        try {
            chatHead.setOnTouchListener(new View.OnTouchListener() {
                private WindowManager.LayoutParams paramsF = params;
                private int initialX;
                private int initialY;
                private float initialTouchX;
                private float initialTouchY;

                @Override public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            // Get current time in nano seconds.
                            long pressTime = System.currentTimeMillis();


                            // If double click...
                            if (pressTime - lastPressTime <= 300) {


                                // Do something else here
                                mHasDoubleClicked = true;
                            }
                            else {     // If not double click....
                                mHasDoubleClicked = false;
                            }
                            lastPressTime = pressTime;
                            initialX = paramsF.x;
                            initialY = paramsF.y;
                            initialTouchX = event.getRawX();
                            initialTouchY = event.getRawY();
                            x = paramsF.x;
                            y = paramsF.y;
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                            paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                            x = paramsF.x;
                            y = paramsF.y;
                            windowManager.updateViewLayout(chatHead, paramsF);
                            break;
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
        }

        chatHead.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                _enable = false;
                handler.post(runnable);
                //              Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                //              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                //              getApplicationContext().startActivity(intent);
            }
        });
    }

    private Handler handler = new Handler();

    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            long downTime;
            long eventTime;

            Log.v("Screen Tapper", "Start Tapping");

            for(int i = 0; i <tapTimes; i++) {
                downTime = eventTime = SystemClock.uptimeMillis();
                MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
                chatHead.onTouchEvent(event);
                Log.v("Screen Tapper", "touchDown ----- "+x+","+y);

                handler.postDelayed(runnable, 10);

                downTime = eventTime = SystemClock.uptimeMillis();
                MotionEvent event2 = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
                chatHead.onTouchEvent(event2);
                Log.v("Screen Tapper", "touchUp ----- "+x+","+y);

                handler.postDelayed(runnable, delayInterval);
            }
        }
    };

    private void getTapInformation() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        tapTimes = prefs.getInt("TAP_TIMES", 1);
        delayInterval = prefs.getLong("Rate", 1);
        Log.v("Screen Tapper", "Get Information");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (chatHead != null) windowManager.removeView(chatHead);
    }

}

我的代码没有发现任何问题,但我觉得chatHead.onTouchEvent(...);不正确

感谢任何帮助。 提前致谢

1 个答案:

答案 0 :(得分:0)

我相信你需要在UI线程上运行触摸事件才能让它们工作,试试这个

view.post(new Runnable() {
   @Override
   public void run() {
      onTouchEvent(event);
   }
});