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
Related
Pretty new to android, so forgive me if this is a dumb question...
So, I'm making an app with a countdown timer that will ring periodically, and then again when the countdown hits zero - simple enough. However, I want the app to keep running even when the user closes the application or the phone is asleep, so that whenever the timer rings, the app will wake up and display an activity showing the time until the countdown is finished. To do this, I'll need to use a service, and lo and behold, the google devs made the AlarmManager service just for me! Sweet!
However, I noticed 2 things:
1) the AlarmManager class has no default constructor, so I'm assuming I can't just extend it and tack some logic on so that I can get all this done in one shot. Ok, cool - I'll just make a service that instantiates AlarmManager at the start, and implement my logic there.
2) In the documentation, I don't see any way of getting either the elapsed time or the remaining time from AlarmManager once it is running.
So, my question is: does this mean that I will need two timers that I start at the same time? Say, an AlarmManager to wake the phone up and call the activity, and a CountDownTimer contained in the service to hold the remaining time and call the alarm ringtone?
Thanks for helping out my clueless ass.
You could extend AlarmManager. However the common way is to get an instance of it, which is running as a system service.
Get the instance using Context.getSystemService(Context.ALARM_SERVICE) and you will be able to register your PendingIntent to that system service, which is independent to your own app. The PendingIntent can either start an activity or send broadcast with some Intent. You don't monitor the elapsed time constantly in AlarmManager. Rather, you calculate the time difference between the current time of your method call, and the desired time to fire your event. And then you set an alarm in AlarmManager with a PendingIntent representing the action you wish to take at that interval, or a time point.
On the other hand, if you want maximum flexibility, run your service as foreground service and listen for system broadcast like ACTION_TIME_TICK, which is fired every minute. Alternatively if you don't run service in foreground you could also run your service with START_STICKY, which guarantees that your service will be restarted after the system kills it (due to sleep or closing app). Think this as a background service that is constantly running. This provides you a lot of flexibility in your implementation.
How to check the current device time in background at regular intervals(say every 1 hour) even after the app is closed using startService() method?Thanks
You don't necessarily need a service, depending on what you want to do with the time you get.
As a general rule, to do something at regular intervals you can use AlarmManager.setInexactRepeating() (or setRepeeating() if you really need a perfect periodicity). Use the PendingIntent of this method to start whatever (BroadcastReceiver, Activity, Service...) when the alarm is triggered.
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.
In one of my application I am providing multiple alarm and its working perfectly fine.
I want to extend that alarm features with some background internet related tasks. When alarm is set for particular given value application start checking for that value from my server at every interval of 30 seconds. whenever same value is returned the alarm goes off. My server is updated with new data at every 30 seconds.
Right now I am setting multiple alarm with broadcast receiver and different pending intent ids. How should I start to implement?
I am confuse between which should I use for this Alarm manager, Services ,Receivers, Handler, AsyncTask?
Any help is appreciated.
Thanks.
You could create a new BroadcastReceiver and set a repeating alarm for every 30 seconds to trigger the receiver when you wish to start polling your server.
The new receiver could then check your server and if the condition is met could call cancel on the alarm manager to cancel the repeating alarm which triggers it.
I'm using an Alarm Manager to trigger an IntentService every hour. However, originally the alarm gets registered when the user runs the app. The problem is that when the user opens the app again it makes the manager create a new service run and if the current service is already running they trip over each other because of the database connection and everything dies!
Another problem is that the alarm service completely stops. Why? I thought the alarm should go on every hour no matter what!
at the very beginning check for a existing database . if it doesn't exist. make a database and start the alarm.
the next time the app start if it found a database it will not start the alarm again. or use any saved value like we use semaphores check for value 0 or 1.. rest functionality of a Alarm manager is supposed to work properly