尝试每5秒钟后模拟触摸事件

时间:2014-03-18 10:04:38

标签: android android-intent

我试图在Android中每隔10秒模拟一次触摸事件作为服务但是从Android主线程中获取无法运行此方法的错误。这是我的服务类代码

int posy=250,posx=250;
Handler mHandler = new Handler();
Instrumentation m_Instrumentation = new Instrumentation();
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}


@Override
public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

}

  @Override
   public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...



    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(5000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                            displayData();
                     //     Toast.makeText(getApplicationContext(), "Got touched at"+Integer.toString(posx)+""+Integer.toString(posy), 1000);




                            m_Instrumentation.sendPointerSync(MotionEvent.obtain(
                                    SystemClock.uptimeMillis(),
                                    SystemClock.uptimeMillis(),
                                    MotionEvent.ACTION_DOWN,posx, posy, 0));
                            m_Instrumentation.sendPointerSync(MotionEvent.obtain(
                                    SystemClock.uptimeMillis(),
                                    SystemClock.uptimeMillis(),
                                    MotionEvent.ACTION_UP,150,200, 0));

                            posx++;
                            posy++;
                        }

                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();

我试图在每10秒钟后在设备上模拟触摸事件但是失败。

这是日志:

03-18 10:15:54.598: E/AndroidRuntime(1008): FATAL EXCEPTION: main
 03-18 10:15:54.598: E/AndroidRuntime(1008): java.lang.RuntimeException: This method 

can not be called from the main application thread

 03-18 10:15:54.598: E/AndroidRuntime(1008):    at 
  android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1641)

  03-18 10:15:54.598: E/AndroidRuntime(1008):   at android.app.Instrumentation.sendPointerSync(Instrumentation.java:926)
  03-18 10:15:54.598: E/AndroidRuntime(1008):   at com.javacodegeeks.android.androidserviceexample.MyService$1$1.run(MyService.java:68)
 03-18 10:15:54.598: E/AndroidRuntime(1008):    at   android.os.Handler.handleCallback(Handler.java:725)
 03-18 10:15:54.598: E/AndroidRuntime(1008):    at android.os.Handler.dispatchMessage(Handler.java:92)
  03-18 10:15:54.598: E/AndroidRuntime(1008):   at android.os.Looper.loop(Looper.java:137)
  03-18 10:15:54.598: E/AndroidRuntime(1008):   at android.app.ActivityThread.main(ActivityThread.java:5041)
 03-18 10:15:54.598: E/AndroidRuntime(1008):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-18 10:15:54.598: E/AndroidRuntime(1008):   at    java.lang.reflect.Method.invoke(Method.java:511)
 03-18 10:15:54.598: E/AndroidRuntime(1008):    at     
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  03-18 10:15:54.598: E/AndroidRuntime(1008):   at    

 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 10:15:54.598: E/AndroidRuntime(1008):     at    dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您不能使用非ui线程中的sendPointerSync方法。因为android ui库不是线程安全的。

因此,对该方法的所有调用都必须来自主线程。你应该为此目的使用android计时器。绝对不需要创建新线程。它非常易于使用,并且将在具有相同感知效果的同一线程上执行。

希望这有帮助。
祝你好运。

修改
这是你要求的好example