How can I run a scheduled task every 60 seconds? - android

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.

Related

Periodic Task with the use of Work Manager API in android

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.

Periodic task and notifications when the application is closed

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?

Schedule job after 12 hours with application is running in background.

I am trying to schedule a job service when i receive BOOT_COMPLETED. Job is getting scheduled immediately and again getting scheduled periodically.
Here depending on some local criteria some time i need to delay the scheduling of job so i can call scheduleJob of scheduler after this delay.
My app does not have any UI and always runs in background , to provide delay i tried timer task , handler etc but these only worked in foreground conditions.
As my app is running in background these did not helped me .
I again tried starting intent service as foreground service and in onHandleIntent trying to sleep the thread for specified time.Once sleep time is over i am scheduling the job and stopForgroundService . Max delay i need to handle is 24 hours.
Is this correct approach to provide delay of max 24 hours and hold it using foreground service or is there any other way i can achieve this.
IntentService, TimerTask

Use LocalService or AlarmManager for a repeating and time consuming task

Currently, I have a stock market app. I would like to perform the following stock alert task code, even when my stock market app is being closed.
Perform a list of stock quotes retrieval, through HTTP network protocol every 30 minutes. Notify user through notification bar if certain stock hits price alert.
I was wondering, should I use
LocalService with a long running thread, which sleep 30 minutes within an infinity while loop
Schedule an every 30 minutes task to AlarmManager
My concern is
If I am using LocalService, let say the user kill the stock market app explicitly through Settings -> Apps -> Running -> Stop, the service will be killed as well. The stock alert task code will no longer running.
Can AlarmManager ensure the stock alert task code will always executed even user kills the stock market app? However, is it suitable for AlarmManager to execute such time consuming task?
I was wondering, should I use LocalService or AlarmManager in my case?
Thanks.
I recommend you to use AlarmManager to send a pending intent to start the service for every 30mins. And the service does its job and calls stopSelf(). Keeping service idle for very long time is waste of system resources.

AlarmManager problems when changing the system clock

I have a service that updates some data every minute. When I change the system clock of the phone or the emulator, the timer executes immediately n times with no delay between them.
Let's suppose that it is 10:00 pm. If I change the time to 11:00pm, the timer runs 60 times one by one with no delay between each run. My service generates HTTP requests so it'll trigger 60 request one by one for 4-5 secs.
What is wrong? I have got the same issue with the AlarmManager too. How can I prevent this behavior?
There is an issue for this
http://code.google.com/p/android/issues/detail?id=17486
if u change system time before u have to cancel PendingIntent and restart it

Categories

Resources