I have been looking for the best way to execute a periodic task every 30/60 seconds which connects to a web service and show a notification depending on the response. This periodic task should keep on executing even if the application is closed (After Destroy).
I have seen these options:
WorkManager: but the minimum time for this is 15 minutes.
AlarmManager: I have read that the resources consumption would be high for my task frequency.
Foreground Service: This could be a good option but I don't like the constantly shown notification in this mode.
Are there any other optimal option to resolve this? Which one do you recommend me?
Related
In my app, I am using ffmpeg-kit and execute a command that record/capture any stream using the internet and writes it to the device's internal storage.
This command or stream can run until the user stops. And from here you can get an idea of this task that this execution can be 10 min, 30 mint or even 1 hr or more depending on the user till he wants to stop or the stream it self complete. e.g: video ends
Now my constraints are regarding this task that the app runs this FFmpeg command in the foreground and background but doesn't need to run when the app is killed by the user or system.
This task is only present and running while the app is running either in the foreground or background.
So now I need a suggestion from experts on which android services are best for use in this case.
I run across a few of them...
IntentService, JobScheduler and WorkManager
I've thought of using WorkManager here but my work is not persistent here and neither I need to run the FFmpeg and record stream while the app closed..so WorkManager is not suitable for this scenario.
None of those is the right answer. IntentService isn't a solution, as background services get killed in 2 minutes. JobScheduler would have the same problem- processing time limitations. As would Workmanager.
What you want is a foreground service. Foreground services are services that can run for long times (they will still also be eventually killed, but can run for hours). Foreground services are launched by using startForegroundService instead of startService, and require the Service to call startForeground and provide a notification that will be in the status bar as long as the service is running.
I have requirement to upload some data to cloud or server at/after every 3 seconds.
I thought of using Work Manager for this Periodic Task of 3 Seconds.
But while started learning the things, I got the below point for the periodic task implementation using Work Manager.
The point is :
Note: The minimum repeat interval that can be defined is 15 minutes (same as the JobScheduler API).
So, Can't I use Work Manager for the interval of 3 Seconds?
Please guide and suggest me the best way to implement this task.
Thanks in Advance.
To implement this task, the best methodology is to use a foreground service and aquire wake locks. That way, the service will always be running and you can send data every 3 seconds.
Workmanager is not suitable for your case here.
I'm developing an android app to check data from a website and notify the user when there is a change. So I want to create a scheduled task that runs in the background and execute a method every 60 seconds.
I searched a lot on the Internet and found:
an Intent, which stops if the app is not running in the foreground.
a JobSchedular where the smallest interval is 15 minutes.
So is there any opportunity to run a task in the background every 60 seconds in android?
Best regards
AndroidMaster
You would need to look into AlarmManager class. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
It's more efficient that way. Check documentation.
I'm working on an application in which I have to do some repeating task at fixed interval (let's say after 2 mins) which should complete even in doze mode. My observations are mentioned below -
Doing repeating task using Alarms (using RTC flag) aren't accurate. Android system batches alarms. If we use RTC_WAKEUP then it is better than RTC, but it shows WAKEUP count in Android Vitals which is not good.
Jobschedulers are useful but will not work for lesser interval like 2 mins. I had tried Firebase Jobdispatcher but that is also not very accurate, I started Job with 2 mins Trigger time but it was varying from 10-20 mins.
Used Handlers and Timers for repeating task in Foreground service. In this scenario Foreground service continue to run in Doze mode but handlers and timer stops repeating task. I read about Handlers.postDelayed() and found that this is also affected by doze mode.
I don't want to acquire WAKELOCK for this repeating task.
Can someone please suggest me some better/clean way by which we can do short interval repeating task in doze mode?
For reference -
Android: What is the best way to make repetitive Background Tasks Android Oreo ready?
How does doze mode affect background/foreground services, with/without partial/full wakelocks?
Not exactly a direct answer but still - I needed to schedule an action every minute in my foreground service (give or take a second).
Fortunately I was capturing sensors in this foreground service too, I made onSensorChanged event check if the right time has passed and act if needed.
I have gone through various discussion threads and tried multiple iterations to have my application scheduled to repeatedly run at fixed intervals. I have been successful too, but starting the thread to get understanding how things work.
I had tried the implementation with ScheduledExecutorService as well as TimerTask and both did not trigger my task at right intervals. But found success with AlarmManager.
Please lead me in right direction to understand why the timing did not work well with ScheduledExecutorService and TimerTask.
The app is required to perform a task on a regular interval. Say, my task takes around 5 minutes to complete, I would like to keep it scheduled so that it runs once in every 30 minutes. When I built the application with ScheduledExecutorService or TimerTask, I found that it does not trigger the task at 30 minutes, but it triggered at 1 hour or even more. So far, with Alarm Manager, it seems to be working fine getting triggered at around 30 minutes(slight variation is still found, but better than the other two).
When I built the application with ScheduledExecutorService or TimerTask, I found that it does not trigger the task at 30 minutes, but it triggered at 1 hour or even more.
More importantly, it will not trigger at all once your process is terminated, which can happen at any point and, from the user's standpoint, should happen quickly, to free up RAM for other apps.
Your trigger delays are probably due to the device falling asleep. Your solution would not only tie up the user's RAM, but also would require you to prevent the device from falling asleep, which is horrible for battery life.
AlarmManager is a far more appropriate solution, as it does not require you to keep a service running or otherwise have a process around. Just be sure to use WakefulBroadcastReceiver as the way you respond to your _WAKEUP event, as the device will want to fall back asleep.