From 810e06c33887833fb2c39d8e72c75bd5c01939b5 Mon Sep 17 00:00:00 2001 From: eguven Date: Mon, 4 Jun 2018 07:30:27 -0700 Subject: [PATCH] Fix starting the download service in the background throw exception This happens when the device screen is locked. This fixes a previous attempt to fix the problem. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=199130325 --- .../demo/SampleChooserActivity.java | 8 ++--- .../exoplayer2/offline/DownloadService.java | 31 +++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java index 7c6dbfc88a..d87fca8e58 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java @@ -94,16 +94,14 @@ public class SampleChooserActivity extends Activity SampleListLoader loaderTask = new SampleListLoader(); loaderTask.execute(uris); - // Ping the download service in case it's not running (but should be). - Intent serviceIntent = - new Intent(this, DemoDownloadService.class).setAction(DownloadService.ACTION_INIT); + // Start the download service if it should be running but it's not currently. // Starting the service in the foreground causes notification flicker if there is no scheduled // action. Starting it in the background throws an exception if the app is in the background too // (e.g. if device screen is locked). try { - startService(serviceIntent); + DownloadService.start(this, DemoDownloadService.class); } catch (IllegalStateException e) { - startForegroundService(serviceIntent); + DownloadService.startForeground(this, DemoDownloadService.class); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java index 21fd541b52..6dae3f70b3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java @@ -160,9 +160,9 @@ public abstract class DownloadService extends Service { * Starts the service, adding an action to be executed. * * @param context A {@link Context}. - * @param clazz The concrete download service being targeted by the intent. + * @param clazz The concrete download service to be started. * @param downloadAction The action to be executed. - * @param foreground Whether this intent will be used to start the service in the foreground. + * @param foreground Whether the service is started in the foreground. */ public static void startWithAction( Context context, @@ -177,6 +177,33 @@ public abstract class DownloadService extends Service { } } + /** + * Starts the service without adding a new action. If there are any not finished actions and the + * requirements are met, the service resumes executing actions. Otherwise it stops immediately. + * + * @param context A {@link Context}. + * @param clazz The concrete download service to be started. + * @see #startForeground(Context, Class) + */ + public static void start(Context context, Class clazz) { + context.startService(new Intent(context, clazz).setAction(ACTION_INIT)); + } + + /** + * Starts the service in the foreground without adding a new action. If there are any not finished + * actions and the requirements are met, the service resumes executing actions. Otherwise it stops + * immediately. + * + * @param context A {@link Context}. + * @param clazz The concrete download service to be started. + * @see #start(Context, Class) + */ + public static void startForeground(Context context, Class clazz) { + Intent intent = + new Intent(context, clazz).setAction(ACTION_INIT).putExtra(KEY_FOREGROUND, true); + Util.startForegroundService(context, intent); + } + @Override public void onCreate() { logd("onCreate");