Count-down timer/alarm API for android - android

I'm learning Android and want to write some kind of count-down timer application, so I get a ring-tone after certain minutes are elapsed. The timer also should work if the user has closed the application or switched to another one. If my application is running, it should be able to display the remaining time.
I've tried with CountDownTimer, but this seems only to work when the phone is activated, but not like the alarms which could ring you up at the morning. What other similar API alternatives are there to activate the device if the time is elapsed?

You can use AlarmManager for this purpose.

count-down and alarm are two very different things (even thou both count time).
Count-down you probably want to run a service and put a notification with the flag ongoing = true updating the value of the time.
Alarm you want to use the AlarmManager (as pointed out by #PgmFreek) that you can schedule a specific time that the system will call an Intent for you.

Related

Is there a maximum limit time for functions in android alarm manager?

I have an app that runs entirely local/offline, and at midnight (using android_alarm_manager and flutter_local_notifications) it does some processing to create and schedule user notifications based on the actual local data of the user and recreate the alarm at midnight, the problem is that after a few days these background processes just stop, if I put it to run every minute it works great when I'm using the device, but when running at midnight its only works on the first days, I even put some logs to see if any errors happened (creating logs in cache and reading them later), but after a few days the app just stops logging too. I think this is caused by android killing the services when it is taking a bit long, but I don't really sure, and if it is, there is another way to make background services on android or the best way is to put my app and user data online and with firebase create and send notifications?
I think I solved the problem, to solve this I used the same packages and the Workmanager, so with AlarmManager I can schedule a alarm to start at midnight that generates and start a periodic work (24 hours) to based on the user data send notifications to the user.
I think this one is a better approach because even android says that WorkManager is the right tool to do background work and alarmmanager is for alarms only.

How do you replace the AlarmManager working in the background in Android Oreo while still setting a specific time of day?

Short version: Alarm Managers no longer work in the background in Android 8 (Oreo), Android people are saying to use Job Scheduler as the alternative, but the job scheduler doesn't allow specific times of the day like Alarm Manager did so well.
Longer version: I have a "reminder" notification that goes off at a specific time of day set by the user. Up to this point, I've used an alarm manager to post the notification at the set time. This sends the user a notification to the user even if the app is in the background. But in Android Oreo / 8.0, this isn't allowed anymore. Most background services, receivers, etc. are fairly limited. I've set up a job scheduler, but doesn't allow for an exact time. Periodic settings in the job scheduler cannot be set ahead to a specific time. So despite Android's claims that job scheduler is the intended replacement for alarm manager, it doesn't seem to do one of the key things people use it for, setting at a specific time of the day. Does anyone know of a way to do this in Android Oreo in a way that it works even when the app is in the background?
I've tried sync adapters (need the alarm manager for that too!), handlers (nothing for a specific time), playing around with the different settings for the job service (periodic, minimumLatency, etc.), resetting the notification after the last one finished (can't do that in the background either!), and still haven't found a valid solution.
Is there anything that works like the job scheduler (i.e. Android 8/Oreo lets you run in the background, but that lets you schedule an exact time of the day, on a daily basis?
Alarm Managers no longer work in the background in Android 8 (Oreo)
I am not aware of anything regarding AlarmManager that changed in Android 8.0. Feel free to point to documentation or an issue tracker entry to validate your claim. Or, file your own issue with a reproducible test case.
But in Android Oreo / 8.0, this isn't allowed anymore.
Sure it is, at least as well as it worked in Android 6.0 through 7.1. Doze mode and app standby have screwed with AlarmManager, but that's not new to Android 8.0.
Most background services, receivers, etc. are fairly limited.
They can still raise a Notification, which is what your question indicates that you want to do.
Does anyone know of a way to do this in Android Oreo in a way that it works even when the app is in the background?
Use AlarmManager. Since you are writing what amounts to an alarm clock app, use setAlarmClock().
one solution i can suggest may be solved your problem.
you should start job schedule 15 minute repeater mode and call every minute one broadcast where you can check this current time is your specific time of day or not.
if yes then send notification.

Service or Alarm manager

I am making a simple android application, in which I have to notify the user about some status like reminder a user to read some article. the user schedules for a reminder and when the reminder show the message and when he tap on it, the application opens the article.
So, my question is that, should I use service for this purpose or alarm manager ?
Always use the AlarmManager to run your code at a given point in time. Money quote:
The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
Alarm manager would be lighter.
See the difference between both as follows.
Use services when you need continuous operation that runs indefinitely in the background.
Use alarms when you need to perform a certain (and short) task at some point in the future, but stay idle until then.
I am new to android too and in the learning phase so please pardon my ignorance if I am wrong but if you want your application to run in the background continuously to check for the updates and set off an alarm when there is some update, then you will need Service as well as AlarmManager. In this case, you will need to ask yourself, whether to use AlarmManager or Handler thread. But again, in case of HAndler thread, you will have to look for WakeLock handling for keeping the handler running while the phone screen is off (CPU sleep mode). In this case AlarmManager wins as it has handled wakelocks internally. But if the repetition time is small then using Handler proves to be benefitial.
Please correct me in case I am wrong or missed something so that I too can learn something new.

Android-Checking current device time in background

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.

Android, set custom timing of AlarmManager... Please advise

Hi I need to set AlarmManager to run a reminder for me to take medication. I need to repeat it by custom amount of days and custom amount of times to take in the day.
So is there an efficient way to set the AlarmManager or CommonsWare's Implementation of the AlarmManager to remind me "twice a day starting at 9AM for the next 5 days" to remind me to take medication? Pls advice and tnx in advance for any constructive help in sample code and in relevant tutorials.
I haven't looked into Mark's AlarmManager implementation, but there is no way, in general, to get the bare AlarmManager to do what you are trying to do. You can schedule a single alarm, at a specific time, or a repeating alarm, that repeats at fixed intervals. If you want something that handles complex schedules like the one you describe, you'll have to write or find code that does it.
You want to use a PendingIntent with the AlarmManager. The idea is to schedule the pendingIntent with the alarmManager, have that trigger an intentService or broadcast, setup another pendingIntent with the alarmManager for the next desired event. You want to keep in mind that you'll need the BOOT_RECEIVED permission in case the user reboots their device. I have complex scheduling in Audio Control and this is exactly what I do.
Here is a pretty decent tutorial of what I mean:
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
You need to schedule an alarm to the next time you want to take the medicine - according to your algorithm (for example if its twice a day, and you got to the pending intent callback for the first time today, then schedule the next alarm to start after [6,7,8,9,10...] hours).
You will need to save both last time of the alarm launch and the user settings in shared prefs/file/DB.
You need to handle process down (android killed it or the device was rebooted). In the case of device reboot you should use the boot receiver to start your service, but you need to remember that from android 3.1 the user has to use at least one time the GUI in order for you to intercept the boot completed receiver. The boot completed receiver should look when was the last time that the alarm launched, and according to the user settings set the next alarm launch.
In the case of android killed your service, you will need to make research, i can't help here.
see example

Categories

Resources