From 536bd68294af4151a71ad8ee076eef980333c279 Mon Sep 17 00:00:00 2001 From: eguven Date: Fri, 1 Jun 2018 08:25:29 -0700 Subject: [PATCH] Fix starting the download service in the background throw exception This happens when the device screen is locked. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=198875192 --- .../exoplayer2/demo/SampleChooserActivity.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 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 5524f98257..7c6dbfc88a 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 @@ -95,8 +95,16 @@ public class SampleChooserActivity extends Activity loaderTask.execute(uris); // Ping the download service in case it's not running (but should be). - startService( - new Intent(this, DemoDownloadService.class).setAction(DownloadService.ACTION_INIT)); + Intent serviceIntent = + new Intent(this, DemoDownloadService.class).setAction(DownloadService.ACTION_INIT); + // 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); + } catch (IllegalStateException e) { + startForegroundService(serviceIntent); + } } @Override