As the title suggests, I need to run something on the UI thread. How do I access this thread, or the main activity that runs in it, within my processing sketch?
I created a sketch that needs to get data from a service that I have running on the device.My class Observer registers itself with the service for the data callback. When it needs the info, it sends the service a Message. The service does its processing then calls the Observer.recieveData(String data) callback.
However, when I attempt to run my sketch on the android, I get an error:
FATAL EXCEPTION: GLThread 10 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:121) at myPackage.ServiceConnectHelper$IncomingHandler.<init>(ServiceConnectHelper.java:209) at myPackage.ServiceConnectHelper$IncomingHandler.<init>(ServiceConnectHelper.java:209) at myPackage.ServiceConnectHelper.connectToService(ServiceConnectHelper.java:76) at processing.android.test.visualizersketch.VisualizerSketch$Observer.<init>(VisualizerSketch.java:150) at processing.android.test.visualizersketch.VisualizerSketch.setup(VisualizerSketch.java:54) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid3D$A3DRenderer.onDrawFrame(Unknown Source) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1454) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1198)
From what I can tell, this occurs because it is trying to do message handling in the GL thread, which does not have access to the message looper. This code needs to be executed in the UI thread (via Activity.runOnUiThread(Runnable r)). If I were writing a stand-alone android app this would be straightforward, but since I am in a processing sketch, how do I access the main activity, or the thread it runs in?