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
Related
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.
In my app, I use AlarmManager to start Foreground Service to do a job at specific time in the future, then call stopForeground(true) when the job done.
I got a problem on below case.
Job 1 : Time scheduled at 10:00 AM, service starts and do a long task. It take about 5 minutes to complete.
Job 2: Time scheduled at 10:01 AM, service starts and do a slightly task. It take only 1 minute to complete.
As you can see, when time is 10:02 AM, job2 completed and job1 has not completed yet. So if I call stopForeground(true) from job2, it will dismiss the notification and also stop the foreground, job1 is impacted and running as background now.
So I consider whether I can know if an "instance" job is running foreground and I should not call stopForeground(true).
Sorry for my bad English.
Are you using just one Service with two jobs or two separate services?
Check that the NOTIFICATION_IDs you're using to startForeground() are different each time, otherwise I guess JOB2 will always dismiss the notification for JOB1 as well
Recently, I have been working on scheduling repeated tasks in background threads in Android application. I started with AlarmManager but due to its inaccurate triggers at specified intervals, I decided to use other JAVA and Android APIs.
I started with Timer and then ScheduledExecutorService and lastly ScheduledThreadPoolExecutor.
The problem with Timer is that it does not always run all the background tasks at right intervals. I had used it to run 3 background repeating tasks out of which only 2 were getting triggered. The third task was triggered after a couple of hours and to catch up with all the previous misses, it was triggered within 10ms (although the interval was 5 mins)
ScheduledThreadPoolExecutor is more accurate in triggering the repeated background tasks. But the problem with it is that it does not trigger the background tasks when the device is in sleep mode (screen off). The same is the behavior with Handler.postDelayed() which pauses the execution of tasks when the device is in sleep mode.
Now, I have reached a dead end. I am looking for a solution that could help me run my background tasks concurrently and without any interruption due to device going to sleep mode.
Please advice.
I am working on an application which triggers an action (say toast message) every 10 minutes after the screen is ON and stops the action after the screen is OFF.
I have used TimerTask for this purpose.
Shall I start using AlaramManager instead of TimerTask or shall I keep using TimerTask ?
I know the difference between the two but can't figure out which to use.
Cant' agree with the nikis' answer
Timer and AlarmManager are solutions addressed to satisfy different needs.
Timer is still a "task" that means this is a thread of your application that means that some component of your application must be running on device to keep timer alive.
If you set timer for 10 minutes events - you can't be sure if your application will not be disposed by system in some moment. If device will be turned into the sleep mode your timer can be stopped. To prevent behavior like that you have to use PowerLock's and drain battery
AlarmManager is system service (runs outside your application) that means that the pending intent will be sent even if your application is killed after setting the alarm.
Some examples:
You have to blink some "led" on the view every 1 s - use Timer - you need it only when application is in foreground, there are short intervals - no point in using AlarmManager for task like that.
You have run some task once after 10 s - Handler.postDelay(); will be the best solution for that, and the job will be done on main thread (UI).
You have to check every 10 minutes if there is some new content on device that you are supposed to push to the server - use AlarmManager - your application does not need to be alive all the time, just let system to start job you want every 10 minutes - that's all.
In most cases you should definitely use AlarmManager, because (from the docs):
The AlarmManager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the AlarmManager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes.
Although you don't need to fire any event while screen is off, AlarmManager still saves the battery by grouping alarms, when you use setInexactRepeating (but this is not important for you, because your interval is 10 minutes). And moreover, it can fire an event is app is not running. I vote for AlarmManager, because it's good practice, but considering your conditions, you can leave Timertask.
BTW, you can also use Handler, which I believe will be the best choice.
I need to fetch news/event updates from the server at regular intervals like for every 20mins in my Android App. AFAIK Intent Service and Broadcast Receiver combination will be better than using Service As I am not going to communicate with the running Service. In order to fetch events at regular intervals I know 2 options
1) Using Timer Task ScheduleAtFixedRate, I am going to start IntentService, which will fetch events once & broadcast if any updates and destroy itself. After Given Interval, IntentService will be again fired by TimerTask
2) Simply starting Intent Service at the start of app and within Intent Service onHandleIntent method Starting a TimerTask ScheduleAtFixedRate. If this is preferred way, How and when I cancel Timer Task and When the Intent Service is going to get Destroyed.
or I have to use Alarm Manager. Note I need these updates as long as I am using the App, also I need updates for every 20 to 30 mins not for every 1 or 3 mins.
Any body please suggest me, Thanks in Advance.
Go for AlarmManager. I have tried TimerTask before, it does not work properly in some devices and get killed after some time.