From 76561865f404f09f026d79181a7795c00ad98064 Mon Sep 17 00:00:00 2001 From: Trevor Johns Date: Fri, 17 Mar 2017 14:30:42 -0700 Subject: BluetoothAdvertisements: Update for background service restrictions Ensure that background advertisement service is running as a foreground service so that it is exempt from background service execution limits. Test: manual Bug: 35451065 Change-Id: I911bb3c3c1e097f3d27d11cf83aa152f01116e97 --- .../bluetoothadvertisements/AdvertiserService.java | 31 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'connectivity/bluetooth') diff --git a/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java b/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java index 0cc3ff03..a3138803 100644 --- a/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java +++ b/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java @@ -1,5 +1,7 @@ package com.example.android.bluetoothadvertisements; +import android.app.Notification; +import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothManager; @@ -25,6 +27,8 @@ public class AdvertiserService extends Service { private static final String TAG = AdvertiserService.class.getSimpleName(); + private static final int FOREGROUND_NOTIFICATION_ID = 1; + /** * A global variable to let AdvertiserFragment check if the Service is running without needing * to start or bind to it. @@ -34,7 +38,7 @@ public class AdvertiserService extends Service { public static boolean running = false; public static final String ADVERTISING_FAILED = - "com.example.android.bluetoothadvertisements.advertising_failed"; + "com.example.android.bluetoothadvertisements.advertising_failed"; public static final String ADVERTISING_FAILED_EXTRA_CODE = "failureCode"; @@ -72,6 +76,7 @@ public class AdvertiserService extends Service { running = false; stopAdvertising(); mHandler.removeCallbacks(timeoutRunnable); + stopForeground(true); super.onDestroy(); } @@ -125,6 +130,8 @@ public class AdvertiserService extends Service { * Starts BLE Advertising. */ private void startAdvertising() { + goForeground(); + Log.d(TAG, "Service: Starting Advertising"); if (mAdvertiseCallback == null) { @@ -134,11 +141,29 @@ public class AdvertiserService extends Service { if (mBluetoothLeAdvertiser != null) { mBluetoothLeAdvertiser.startAdvertising(settings, data, - mAdvertiseCallback); + mAdvertiseCallback); } } } + /** + * Move service to the foreground, to avoid execution limits on background processes. + * + * Callers should call stopForeground(true) when background work is complete. + */ + private void goForeground() { + Intent notificationIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, + notificationIntent, 0); + Notification n = new Notification.Builder(this) + .setContentTitle("Advertising device via Bluetooth") + .setContentText("This device is discoverable to others nearby.") + .setSmallIcon(R.drawable.ic_launcher) + .setContentIntent(pendingIntent) + .build(); + startForeground(FOREGROUND_NOTIFICATION_ID, n); + } + /** * Stops BLE Advertising. */ @@ -220,4 +245,4 @@ public class AdvertiserService extends Service { sendBroadcast(failureIntent); } -} \ No newline at end of file +} -- cgit v1.2.3