Skip to content

Commit

Permalink
Start foreground service in a way supported on Android 8
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed Oct 3, 2018
1 parent ff5ecdd commit ccb2071
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/org/altbeacon/beacon/BeaconManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public boolean checkAvailability() throws BleNotAvailableException {
* @param consumer the <code>Activity</code> or <code>Service</code> that will receive the callback when the service is ready.
*/
public void bind(@NonNull BeaconConsumer consumer) {
/*
if (!isBleAvailable()) {
LogManager.w(TAG, "Method invocation will be ignored.");
return;
Expand All @@ -415,6 +416,7 @@ public void bind(@NonNull BeaconConsumer consumer) {
LogManager.w(TAG, "This device does not support bluetooth LE. Will not start beacon scanning.");
return;
}
*/
synchronized (consumers) {
ConsumerInfo newConsumerInfo = new ConsumerInfo();
ConsumerInfo alreadyBoundConsumerInfo = consumers.putIfAbsent(consumer, newConsumerInfo);
Expand All @@ -430,7 +432,13 @@ public void bind(@NonNull BeaconConsumer consumer) {
else {
LogManager.d(TAG, "Binding to service");
Intent intent = new Intent(consumer.getApplicationContext(), BeaconService.class);
consumer.bindService(intent, newConsumerInfo.beaconServiceConnection, Context.BIND_AUTO_CREATE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
this.getForegroundServiceNotification() != null) {
mContext.startForegroundService(intent);
}
else {
consumer.bindService(intent, newConsumerInfo.beaconServiceConnection, Context.BIND_AUTO_CREATE);
}
}
LogManager.d(TAG, "consumer count is now: %s", consumers.size());
}
Expand Down

0 comments on commit ccb2071

Please sign in to comment.