Reputation: 2358
I am using a ScheduledExecutorService to run a Runnable periodically. In the Runnable I have registered a SensorEventListener. I noticed that the SensorEventListener callbacks gets called on the main thread rather than a thread from the thread pool of the ScheduledExecutorService. I want to handle the callbacks for the sensor events off of the main thread. It looks like I am able to pass a Handler object when calling registerListener() of the SensorManager class and the callbacks will be run on the thread that the Handler is from.
Is there a way to get a reference to a Handler of a ScheduledExecutorService?
Upvotes: 2
Views: 2659
Reputation: 63955
It's hard. A regular ExecutorService
does not have a Looper
and you can not have a Handler
in such a thread.
A Looper
is an infinite loop that dequeues and executes events. So if you schedule that your executor is blocked. You can probably implement your own executor based on the event handling of a Looper but I guess that's not what you want.
To use the sensor from the background you would create a HandlerThread
. That's a background thread running a Looper and therefore it can have a Handler
.
Small example
private HandlerThread mHandlerThread;
private Handler mBackgroundHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandlerThread = new HandlerThread("SomeNameHere");
mHandlerThread.start();
mBackgroundHandler = new Handler(mHandlerThread.getLooper());
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
// I can do blocking network here.
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandlerThread.quitSafely();
}
Upvotes: 5
Reputation: 2485
Handler always 'handles' thing in the thread it was created, so just create a new one in your ExecutorService
. Just use default Handler constructor
Upvotes: 0
Reputation: 1912
You can try Handler handler = new Handler(Looper.myLooper());
in the background thread (Where your ScheduledExecutorService
is running) and then pass the instance to the SensorManager
.
Upvotes: 0