Move ScanJob startup work to non-UI thread in case of hang on state restore
@@ -54,12 +54,17 @@ public class ScanJob extends JobService {
@Override
public boolean onStartJob(final JobParameters jobParameters) {
+ // We start off on the main UI thread here.
+ // But the ScanState restore from storage sometimes hangs, so we start new thread here just
+ // to kick that off. This way if the restore hangs, we don't hang the UI thread.
+ new Thread(new Runnable() {
+ public void run() {
if (!initialzeScanHelper()) {
LogManager.e(TAG, "Cannot allocate a scanner to look for beacons. System resources are low.");
- return false;
+ ScanJob.this.jobFinished(jobParameters , false);
}
ScanJobScheduler.getInstance().ensureNotificationProcessorSetup(getApplicationContext());
- if (jobParameters.getJobId() == getImmediateScanJobId(this)) {
+ if (jobParameters.getJobId() == getImmediateScanJobId(ScanJob.this)) {
LogManager.i(TAG, "Running immediate scan job: instance is "+this);
}
else {
@@ -111,6 +116,9 @@ public class ScanJob extends JobService {
LogManager.i(TAG, "Scanning not started so Scan job is complete.");
ScanJob.this.jobFinished(jobParameters , false);
}
+ }
+ }).start();
+
return true;
}
