Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always deliver scan/ranging results on the main thread #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/altbeacon/beacon/BeaconIntentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.altbeacon.beacon.service.MonitoringData;
import org.altbeacon.beacon.service.RangingData;

import android.os.Handler;
import android.os.Looper;

import android.annotation.TargetApi;
import android.app.IntentService;
import android.content.Intent;
Expand All @@ -44,6 +47,19 @@ public BeaconIntentProcessor() {

@Override
protected void onHandleIntent(Intent intent) {
//Make sure we always deliver results on the main thread
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onHandleIntent() always gets executed on a worker thread, as this is the expected behaviour of an IntentService (see documentation).

It's probably better not to mess with Android callbacks by calling them directly.
It might be safer to instantiate the Handler in the onCreate() and add a new method that posts to the main thread the calls to the listeners.

if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
Handler mainHandler = new Handler(Looper.getMainLooper());

Runnable myRunnable = new Runnable() {
@Override
public void run() {
onHandleIntent(intent);
}
};
mainHandler.post(myRunnable);
return;
}
LogManager.d(TAG, "got an intent to process");

MonitoringData monitoringData = null;
Expand Down