Reputation: 29
I am trying to simulate touch event after every 10 sec as a service in Android but getting the error that can't run this method from main thread in Android. Here,is my code of the service class
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();
I am trying to simulate touch event on device after every 10 sec but failing.
And here is the log:
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)
Upvotes: 1
Views: 377
Reputation: 7057
You can't use sendPointerSync
method from a non-ui thread. Since android ui library is not thread safe.
Hence all calls to that method must be made from main thread. You should use android timers for that purpose. There is absolutely no need to create a new thread. It's really easy to use, and will be executed on the same thread with same perceived effect.
Hope this helps.
Good luck.
Edit:
Here is a good example as you asked for.
Upvotes: 1