Android: Fire all missed alarms when device turns on - android

In my application the user has set some reminders and I have to alarm them when the time has come. Currently I'm using AlarmManager.RTC as a type of AlarmManager
am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
it in working as per instruction
// Sets an alarm - note this alarm will be lost if the phone is turned
// off and on again
is there any way the I can fire missed alarms when I turn on the device?
Please note that I don't want to wakeup the device, I just wanted to remind them when they turn on the device.
PS: I have read the documentation http://developer.android.com/reference/android/app/AlarmManager.html#RTC, but I didn't find my option.

is there any way the I can fire missed alarms when I turn on the device?
Step #1: Have some way of tracking, yourself, what is missed, such as by tracking event status in a database.
Step #2: Set up a BOOT_COMPLETED BroadcastReceiver to find when the phone is rebooted. In there, schedule any alarm(s) that are still in the future, and decide what to do about alarms that occurred in the past but were missed (e.g., raise a Notification pointing out the missed events).

You would have to do this manually.
One way to do this would be to listen for the phone shutdown event:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
and before the device shuts down, save the shutdown time.
Then listen for device bootup:
<action android:name="android.intent.action.BOOT_COMPLETED" />
and write your own logic that determines which alarms would have fired during that downtime, and fire those alarms immediately with AlarmManager.

AlarmManager will alarm all of your alarms till your phone is on.
Registered alarms are retained while the device is asleep (and can
optionally wake the device up if they go off during that time), but
will be cleared if it is turned off and rebooted.
Now that only way fits is to kepp tracks through database.
Put an additional class that extends BroadcastReceiver. Where in its OnReceive() method you can re-generate your alarms when device reboots. (obviously if you have some track, i.e database).
Setting BroadcastReceiver for your purpose can be seen here How to start an Application on startup?.
Hope this helps.

Register broadcastReceiver in your Manifest.xml
<receiver
android:name=".AlarmRestartReceiver"
android:enabled="true">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this, listen event when device wake up.
override fun onReceive(context: Context, intent: Intent?) {
if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
// re-register all alarm here..
}
}

Related

BOOT_COMPLETED Broadcast not received when app was killed

I'm stuck at the development of our app. I want to receive the BOOT_COMPLETED broadcast so I can reschedule some Alarms. So I start my app, schedule the alarms and then reboot my device (Android 10). Everything works as expected, ie after reboot, my BroadcastReceiver is started and my alarms are rescheduled.
But if I stop my app before reboot (ie not just put it in background, really kill it), it doesn't receive the BOOT_COMPLETED broadcast anymore ...
I have the respective permissions in my manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I have the receiver in my manifest enabled with the respective actions
<receiver
android:name=".receivers.CustomBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="true"
android:directBootAware="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And this is my CustomBroadcastReceiver class
class CustomBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(javaClass.simpleName, "onReceive ${intent.action}")
... //reschedule alarms read from SQLite
}
}
I can see the respective Log message after reboot, when the app was running before reboot. But I don't see the log, when the app was killed before the reboot ... Anything I'm missing here?
Yes, I'm aware, there are many other questions regarding BOOT_COMPLETED but they all more or less say, do it like shown above, then it will work. And, well, I know it in principle does work, because I see the receiver being called. But just not, when the app was once killed. If I start the app manually, put it in background and reboot the device, it works as expected ...
Use JobScheduler instead of alarms for this. JobScheduler jobs can be set as persistent across boot, so you don't have to do the bootup wiring yourself.

Local notifications not showing up when device is rebooted (or similar state) Android

I'm a bit new so I have a simple question, I have created a Xamarin.Android app and I have used local notifications with alarm manager, broadcast receivers, etc to schedule them, the problem that I have is that my scheduled notifications are not being showed when the device is rebooted, or completely off state and turn on again. My questions are:
Is that a problem inside Android?
otherwise
How can I solve it?
I hope help, thanks
By default, all alarms are canceled when a device shuts down.
https://developer.android.com/training/scheduling/alarms
By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.
You need to monitor for BOOT_COMPLETE and re-set your alarms.
https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".SampleBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
Receiver:
class SampleBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// Re-set the alarm here.
}
}
}

Receive alarm in spite of that device has been restarted

I want to make an alarm clock. I've done it but it works only if device wasn't restarted.If it was I just don't get any alarm. What should I do to receive my alarm no matter has device been restarted or not?
That's how I set alarm time:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pi1);
This is what I tried in a similar situation:
Make your broadcast receiver detect the system restart. Now, as soon as your system restarts, your broadcast receiver will detect it and you can perform your task. Like, in my project, I ran a query onto my SQLite database and retrieved all the alarms that have not executed and then executed them according to my requirements and updated the database with the same.
You can try something similar to it.
Code to retrieve alarms:
Add your Broadcast Receiver with the following attributes:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This BroadcastReceiver will detect your Android device's restart and execute the onReceive() method of the BroadcastReceiver. In the onReceive() method, retrieve all the alarms according to your criteria and then process them further.
To learn further about how to retrieve records or tuples from the SQLite Database go through the following link:
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow

Schedule AlarmManager Android

Can I schedule alarm manager (.set() methode) in android to a time which is one month later the current time
will it be alive for that mutch of time ?
what will be the OS effects on this alarmManager ?
Tip for a case when user reboots his phone. You can declare BroadcastReceiver with the following intent-filter:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
and re-schedule your alarm in onReceive(...) of your receiver class. Intent will be delivered to the receiver after user reboots his phone. Don't forget to add the following permission to your manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Or you can use the library that will handle such cases for you:
https://github.com/commonsguy/cwac-wakeful
Theoretically yes, If the user keeps his phone on for that long, however the alarm doesn't survive reboots and will therefore not be scheduled when the phone restarts.

constantly watching the battery?

I dont know if this can be done but right now i have a service that has an alarm manager which refreshed the battery every 5 mins to check the battery % and alert of the battery gets below a certain %. Is there a better way to watch the battery that does not require an alarm manager and refreshes when the battery changes because it seems better than refreshing after a certain amount of time
tyczj,
I would create a BroadcastReceiver to receive the following Broadcast: ACTION_BATTERY_CHANGED. This way you aren't constantly polling the battery unnecessarily.
Additional:
It would be registered like this:
<receiver android:name=".BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
You can listen to the systems broadcast intents:
ACTION_BATTER_CHANGED
ACTION_POWER_CONNECTED
ACTION_POWER_DISCONNECTED

Categories

Resources