Check to call stopForeground(true) - android

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

Related

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

TimerTask vs AlaramManager, Which one should I use?

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.

Android: CountDownTimer in BroadcastReceiver

I have an app that uses a CountDownTimer inside a BraodcastReceiver. The CountDownTimer can be for upwards of 1 hour. The timer shows the countdown in the Notifications area (second intervals).
Some users have reported that the app seems to hang on long count downs. The CountDownTimer is triggered by a widget.
Does anyone know if a CountDownTimer can be stopped and reclaimed by the OS?
The alternative would be to set a recurring alarm at 1 second intervals which runs a service. Is there a better option?
Does anyone know if a CountDownTimer can be stopped and reclaimed by the OS?
Your process will be.
The alternative would be to set a recurring alarm at 1 second intervals which runs a service.
That's not an option in any practical sense, if by "recurring alarm" you mean AlarmManager. AlarmManager is not designed for every-second events.
Is there a better option?
This is one of the few cases that justifies a foreground service. Since you have a Notification anyway, and since your AlarmManager approach would keep the service around constantly anyway, you may as well dispense with the AlarmManager and use startForeground() to keep the service around. Update the Notification that you are using with startForeground(), and use ScheduledExecutorService to get control every second on a background thread.
When the countdown is done, call stopForeground() and stopSelf() to get rid of it all.

Android: Use AlarmManager to repeatedly call service that sometimes lasts longer than the repeat interval

I want to start a service every 15 minutes. Most of the time it will quit in about 30 seconds, but there will be times the service lasts longer than 15 minutes. I don't think it would be good for the alarmmanager to start the service if the previous service call is still running. I was considering setting up a flag in onStartCommand to have it simply return without doing anything if the service is running. But I'm not sure if this is the best way to handle it, as onStartCommand returns an int and I don't know what the system needs for this value. Is there a best way to do this?
But I heard from CommonsWare this: "No, it will not create a new service. If the service is already running, it will be called with onStartCommand() again, to deliver the new Intent, but a second copy is not created."
Yeah, that CommonsWare guy, he sure does write a lot... :-)
I don't think it would be good for the alarmmanager to start the service if the previous service call is still running.
If you are using IntentService, this will not be an issue. The command from your second alarm will be enqueued, awaiting onHandleIntent() to wrap up from the first alarm.
If you are not using IntentService, you will need to have smarts in onStartCommand() to determine that your own thread is still chugging along, and therefore you want to skip the command.
But I'm not sure if this is the best way to handle it, as onStartCommand returns an int and I don't know what the system needs for this value.
That doesn't really have anything to do with your problem. Just return super.onStartCommand(), or whatever your current onStartCommand() method is returning.
But what if I don't want the service to start again until the next time its supposed to be called?
Well, you could replace the repeating alarm with one-shot alarms (use set()) and then set up the next alarm as part of wrapping up the work from the previous alarm.

Android: Stopping Alarm at specific time and service clarification

is there any way to stop an alarm at a given time, for example, i started an alarm on 7:45am and it repeats every 5 minutes and then, i wanted to stop that alarm when the time is already 8:00am?
How do i go about this algorithm since alarm manager's cancel() method only accepts an action (PendingIntent)
and Secondly, regarding services, i have an idea to put a checker or an if statement on service to check if its already 8:00a, but im not sure if service always run in the background and if so, does that mean to say that it always checks the time if its 8am? given that meaning of services that once it started i won't stop unless explicitly told to do so.
any of you guys know any way to do this please do share, im kinda confused right now
you will have to start the service when alarm start first time and this service will ring the alarm at 5 min and get the device time and compare it with the time to stop the time
at that (means at 8 AM )stop your service
Why dont you start whatever you need to do at 7:45am with AlarmManager#setRepeating and set another alarm with AlarmManager#set to stop the previous?
AlarmManager#set will be fired off just once, so you can use it cancel the repeating alarm
Edit:
In the implementation of AlarmManager#set, you would retrieve the repeating alarm(AlarmManager#setRepeating) and cancel() it

Categories

Resources