Why are Music Services in Android never affected by doze mode? - android

Can anyone please explain to me at technical level, why does a music player's service run continuously after the screen is turned off. Why is it never affected by doze mode? I want to create a service along similar lines, but it should perform a particular task every 20 seconds continuously.
I would be glad if anyone can explain this. Thanks in advance.

The music player's service runs as a 'foreground service'.
Foreground services for the most part are unaffected by doze when used in combination with a partial wakelock:
Purpose of WakeLock in modern Android?
If your app needs to create a foreground service while the app is in the background:
ContextCompat.startForegroundService()
Inside the service, promote it to the foreground and post the notification by calling:
startForeground()
From the Android docs:
"A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app."
https://developer.android.com/guide/components/services
Typically, a music app will change the foreground service notification from a 'liability' that annoys the user to an asset by displaying an image of the artist, the current track and offer media buttons like play/pause/rewind/forward.
A foreground service should not generally be used to do regularly scheduled work because it will greatly reduce the device's battery life.
Instead, try to use WorkManager, JobScheduler or Firebase Cloud Messaging to wake the device up only when necessary.
Execute frequent tasks using the Work Manager API
But it will not be possible to "poll" or run your code every 20 seconds with these solutions. You really need to find a solution that works with "Doze" instead of fighting against it.

Related

What is the best way to make foreground service for real-time driver location update for ride hailing app in Android studio and kotlin

I am building a ride-hailing app and I need a real-time driver location update even when the app is closed or in background, according to the new android os versions I can't use background services even if I could use it the os might kill it so my best option is to use foreground service with a noticeable notification, my question is, is it possible to use an ongoing foreground service for realtime location updates without being killed?
A foreground service can still be killed, its just less likely to be so. If the user was to open up a couple of memory hogging apps that meant it really needed your apps memory, it can still be killed. There's a priority to what stays in memory, having a foreground service just makes it higher priority than an app with a background service using the same resources. That said, a foreground service is your best bet for short duration updates like that.
Note that there's a difference between "closed" and backgrounded. If the app is backgrounded, a foreground service will continue. If the user terminates the app by swiping it away from recents or force stopping it, the foreground service will also be killed. But the foreground service would allow him to move to another app (like Waze or something) without killing your app unless the phone goes really low on memory.
i have a problem look like you . i am searching a lot and i test
foregroundservice , alarmManager , Worker and ...
none of them isnt working well and suddenly service stoped ! .
in the end i find 1 ways :
1- handle service in server in backened with pushNotificaiton .

How does one implement a background notification service in android when all services except for foreground ones get shut down after some time?

I would love to offer a background notification service(real-time) but can't find a way how to make it work since background services get stopped by system and when I restart them from a receiver I get an error.
I see that other apps have it, but I checked the running services and they don't have any services there. Do you think they do it in some short intervals to check for new notifications ?
Any advice would be helpful.
You have two choices:
1:Foreground Service.
You can keep a service running indefinitely, by promoting your service to a "foreground service". You can only become a foreground service by adding a Notification for the service to the notifications area. Presumably one which allows users to make your service go away. See Service.setForeground.
Poll periodically using WorkManager and AlarmManager
These APIs allow you to schedule periodic work when there is an active internet connection. The basic idea is that you would poll every few minutes to see whether there is stuff to be done.
There are no other options. This is by design. There is no way to lurk in the background constantly without displaying a notification. Android OS developers have put a lot of work into making sure that there is no other way.

What should be used for background tasks instead of foreground service when app is killed?

In my app I want to show notification on exact time when the app is working in background or even it is closed. I used AlarmManager and service with BroadcastReceiver to show notification. The latest versions of android doesn't allow to run service in background after app closed and foreground service is consuming battery, slowing down the device etc.. I wonder if I could use something else can work even app is closed and show the notifications. I've heard of WorkManager and JobScheduler for that kind of operations but can they do the work even if app is closed?
Foreground services is not consuming more battery and not slowing down device, just notification is shown.
And the answer is basically it is not possible to make continuous background service on new android API's. You only can schedule tasks with tools like WorkManager, JobScheduler and so on.
These days I think WorkManager is the answer because Android team is focused on it this year.
Also even continuous foreground services gets killed after some time.... To avoid that 2 things has to be done:
Native battery optimization for app should be disabled.
Phone manufacturer (another battery optimization) has to be also disabled for specific app.....
I know it is worst user experience, and they throwing away half smart things that phone can do, but life is life :)

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?
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.

How to continuously update notification even when app is not running?

I am trying to implement a timer with notification that cannot be swiped away by user. I want to continuously update the timer's time.
I know there are a lot of similar question online. But a lot of them discuss using handler, or a service. All of these won't run when my app is not being used by the user. AlarmManager doesn't work because i want to update every x millisecond.
Thanks.
Use a foreground service
A foreground service performs some operation that is noticeable to the
user. For example, an audio app would use a foreground service to play
an audio track. Foreground services must display a Notification.
Foreground services continue running even when the user isn't
interacting with the app.
There is a limit on how frequently you can update the notifications as well, every MS will cause problems.

Categories

Resources