|
36 | 36 | import android.Manifest.permission;
|
37 | 37 | import android.annotation.SuppressLint;
|
38 | 38 | import android.app.Activity;
|
| 39 | +import android.app.Notification; |
| 40 | +import android.app.Service; |
39 | 41 | import android.app.UiModeManager;
|
40 | 42 | import android.content.BroadcastReceiver;
|
41 | 43 | import android.content.ComponentName;
|
@@ -296,6 +298,35 @@ public static ComponentName startForegroundService(Context context, Intent inten
|
296 | 298 | }
|
297 | 299 | }
|
298 | 300 |
|
| 301 | + /** |
| 302 | + * Sets the notification required for a foreground service. |
| 303 | + * |
| 304 | + * @param service The foreground {@link Service}. |
| 305 | + * @param notificationId The notification id. |
| 306 | + * @param notification The {@link Notification}. |
| 307 | + * @param foregroundServiceType The foreground service type defined in {@link |
| 308 | + * android.content.pm.ServiceInfo}. |
| 309 | + * @param foregroundServiceManifestType The required foreground service type string for the {@code |
| 310 | + * <service>} element in the manifest. |
| 311 | + */ |
| 312 | + public static void setForegroundServiceNotification( |
| 313 | + Service service, |
| 314 | + int notificationId, |
| 315 | + Notification notification, |
| 316 | + int foregroundServiceType, |
| 317 | + String foregroundServiceManifestType) { |
| 318 | + if (Util.SDK_INT >= 29) { |
| 319 | + Api29.startForeground( |
| 320 | + service, |
| 321 | + notificationId, |
| 322 | + notification, |
| 323 | + foregroundServiceType, |
| 324 | + foregroundServiceManifestType); |
| 325 | + } else { |
| 326 | + service.startForeground(notificationId, notification); |
| 327 | + } |
| 328 | + } |
| 329 | + |
299 | 330 | /**
|
300 | 331 | * Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE}
|
301 | 332 | * permission read the specified {@link Uri}s, requesting the permission if necessary.
|
@@ -3234,4 +3265,29 @@ public static Drawable getDrawable(Context context, Resources resources, @Drawab
|
3234 | 3265 | return resources.getDrawable(res, context.getTheme());
|
3235 | 3266 | }
|
3236 | 3267 | }
|
| 3268 | + |
| 3269 | + @RequiresApi(29) |
| 3270 | + private static class Api29 { |
| 3271 | + |
| 3272 | + @DoNotInline |
| 3273 | + public static void startForeground( |
| 3274 | + Service mediaSessionService, |
| 3275 | + int notificationId, |
| 3276 | + Notification notification, |
| 3277 | + int foregroundServiceType, |
| 3278 | + String foregroundServiceManifestType) { |
| 3279 | + try { |
| 3280 | + // startForeground() will throw if the service's foregroundServiceType is not defined. |
| 3281 | + mediaSessionService.startForeground(notificationId, notification, foregroundServiceType); |
| 3282 | + } catch (RuntimeException e) { |
| 3283 | + Log.e( |
| 3284 | + TAG, |
| 3285 | + "The service must be declared with a foregroundServiceType that includes " |
| 3286 | + + foregroundServiceManifestType); |
| 3287 | + throw e; |
| 3288 | + } |
| 3289 | + } |
| 3290 | + |
| 3291 | + private Api29() {} |
| 3292 | + } |
3237 | 3293 | }
|
0 commit comments