Doctor blue
Doctor blue

Reputation: 43

Slow down the auto-off process of the android app after turning off the screen

I created a music app on the android platform but when I let it run for a few minutes when the screen turned off, the app turned off and didn't play the music anymore. So how do I extend the time the application runs when the screen is turned off?

Upvotes: 4

Views: 166

Answers (1)

letsintegreat
letsintegreat

Reputation: 3366

I'm assuming that you are running the Service to play music in the background. So, when OS thinks that the mobile device has to reduce the battery consumption, it just kills your service sometime after it was run.

Now, what you can do to avoid this, is running that in foreground instead. Because then that service is kind of marked as useful by the user so that doesn't get killed unless the user themselves do that.

Find out more about How to set a Service to run in foreground here in the documentation

The above link clearly mentions:

a music player that plays music from a service should be set to run in the foreground

But keep in mind that any service to accomplish any task should run in the foreground only if the user is aware that the task is being accomplished.

You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app.

And for that reason, you must show a notification mentioning that this service is being run in the foreground, to accomplish this task, so that the user can be in knowledge of that.

A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Upvotes: 4

Related Questions