I am developing an app that requires a long running timer service that calls a function after few minutes to send some data to the server. Its a foreground service running in the background on a separate process. I need the service running continuosly in the background until the user presses a button. But the problem I noticed is that the service pauses when device is in sleep mode which is not acceptable as the app is very time critical. Is there any way to make sure the service runs continously in the background? Is partial wake lock a good way to tackle this problem? Please help me as I'm new to android.
Related
My App needs to run in background continuously to send the location updates to the server. I tried Timer , Job Scheduler with background service none of these didn't worked. Its working fine when device is active, after entering into Doze mode the timer which i used is getting paused. When the device is active the Timer is getting resumed.
Checked with this Permission also ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS.
Post OREO there are limits to what you can do on a background thread
Create a foreground service. In which you will put notification for the user while your service is running.
Here is a StackOverflow answer which would help you create that foreground service
Foreground Service
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.
I'm writing an Android application that records video and then runs post processing on recorded video. Since post processing may take a while (up to an hour) I'm scheduling a service - JobService using JobScheduler to run when the phone is connected to power and idle i.e. to run at night.
The job service doing the long-running job on a new thread.
In practice my scheduled service is working VERY slow. It seems that although it runs when system is idle, it gets very little CPU bandwidth. Also my service is being stopped after 10 minutes max by the OS.
I know Android Oreo limits background processing. This is well documented by Google. However I'm wondering if there is a way to run background services when system is idle.
Is there a way to run long background services when system is idle? For example video post processing?
You need to use a Foreground service.
They are one of the few methods left to run long background tasks. The user must know what's happening on his device and he should have the ability to decide to stop it or not.
You can read how to implement basic Foreground Services here, and there are a lot of guides on the internet on how to implement them in an advanced manner.
Remember that even a Foreground Service can be stopped by the OS if the system is in extreme memory pressure and the only things left to destroy are foreground services. So you should always have a recovery procedure if that happens.
My IntentService checks the user's location. I need to keep it alive while the phone is turned off. In this Service I have an infinite while loop. The OS will shut it down after 8 minutes.
What other ways can I do this instead of the AlarmManager?
IntentService is supposed to be executed once. When job is done, it terminates. You may try using ordinary Service and do all work on background thread. But, system can stop this service anyway if it is low on resources. To prevent that, you can use .setForeground() method in Service and Service will not be stopped by system. But, there will be icon in notification tray.
Is the background service limited to 10 minutes, or could it run for hours? And is there any way the user can shut down the background service while it is running?
Depends on the code, you can run the background service forever or close it as soon as the app has been exited. Apart from that, OS itself can close the background service if no action is performed by it for several minutes.