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
This commit is contained in:
eguven 2018-06-04 07:30:27 -07:00 committed by Oliver Woodman
parent 0f1931cbcf
commit 810e06c338
2 changed files with 32 additions and 7 deletions

View File

@ -94,16 +94,14 @@ public class SampleChooserActivity extends Activity
SampleListLoader loaderTask = new SampleListLoader(); SampleListLoader loaderTask = new SampleListLoader();
loaderTask.execute(uris); loaderTask.execute(uris);
// Ping the download service in case it's not running (but should be). // Start the download service if it should be running but it's not currently.
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 // 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 // action. Starting it in the background throws an exception if the app is in the background too
// (e.g. if device screen is locked). // (e.g. if device screen is locked).
try { try {
startService(serviceIntent); DownloadService.start(this, DemoDownloadService.class);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
startForegroundService(serviceIntent); DownloadService.startForeground(this, DemoDownloadService.class);
} }
} }

View File

@ -160,9 +160,9 @@ public abstract class DownloadService extends Service {
* Starts the service, adding an action to be executed. * Starts the service, adding an action to be executed.
* *
* @param context A {@link Context}. * @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 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( public static void startWithAction(
Context context, 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<? extends DownloadService> 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<? extends DownloadService> clazz) {
Intent intent =
new Intent(context, clazz).setAction(ACTION_INIT).putExtra(KEY_FOREGROUND, true);
Util.startForegroundService(context, intent);
}
@Override @Override
public void onCreate() { public void onCreate() {
logd("onCreate"); logd("onCreate");