Clarify that method can only be called after onCreate

And improve error message if access to the notification controller
happens without a base context.

#cherrypick

PiperOrigin-RevId: 736545574
(cherry picked from commit 41722be02e34199543dc92c44e12ff95a35cb378)
This commit is contained in:
tonihei 2025-03-13 10:03:54 -07:00
parent 97606094ca
commit 23468ed55c

View File

@ -481,6 +481,9 @@ public abstract class MediaSessionService extends Service {
* Sets the timeout for a session to stay in a foreground service state after it paused, stopped, * Sets the timeout for a session to stay in a foreground service state after it paused, stopped,
* failed or ended. * failed or ended.
* *
* <p>Can only be called once the {@link Context} of the service is initialized in {@link
* #onCreate()}.
*
* <p>Has no effect on already running timeouts. * <p>Has no effect on already running timeouts.
* *
* <p>The default and maximum value is {@link #DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS}. If a larger * <p>The default and maximum value is {@link #DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS}. If a larger
@ -679,6 +682,7 @@ public abstract class MediaSessionService extends Service {
synchronized (lock) { synchronized (lock) {
if (mediaNotificationManager == null) { if (mediaNotificationManager == null) {
if (mediaNotificationProvider == null) { if (mediaNotificationProvider == null) {
checkStateNotNull(getBaseContext(), "Accessing service context before onCreate()");
mediaNotificationProvider = mediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(getApplicationContext()).build(); new DefaultMediaNotificationProvider.Builder(getApplicationContext()).build();
} }