I know how to use the BOOT COMPLETED Broadcast receiver but I wonder what exactly should i do? How to save the time of the alarms given that i have an unknown number of alarms all set by the user? And how do i restore them in the Broadcast receiver?
Related
My question is similar to this one:
battery receiver fully charged doesnt work
. That question has no answers.
Here are the approaches I have thought of so far:
Approach 1: Register a BroadcastReceiver in the AndroidManifest.xml for ACTION_BATTERY_CHANGED.
This won't work because of the following reason:
I cannot register a BroadcastReceiver in the AndroidManifest.xml to listen for ACTION_BATTERY_CHANGED because it is a Sticky Intent as stated here: https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED.
Approach 2: Register a BroadcastReceiver programatically in the MainActivity.java's onCreate() method.
Issue with this approach is : Avoid registering duplicate broadcast receivers in Android
In addition to the above issue, I do not understand the scope of broadcast receivers properly. To be more precise, I don't understand what happens to the broadcast receivers that an app has registered when the app is:
1) Destroyed by the Android system by calling it's onDestroy() method
2) When the app is force stopped by the user via the Application Manager setting.
Could someone please explain it please?
Approach 3: Use a work around that uses SharedPreferences and a BroadcastReceiver for listening to device reboot.
Have a shared preference that let's me know if I have previously registered a broadcast receiver for ACTION_BATTERY_CHANGED. If yes, don't create a new one, else, create a new broadcast receiver for ACTION_BATTERY_CHANGED. As pointed out in the link in Approach 2, the value in the SharedPreference becomes invalid upon device reboot, so have a separate broadcast receiver to create the receiver again on reboot and update the shared preferences to reflect the same.
Although, approach 3 may work, I feel it is a cumbersome way. Is there a simpler approach?
Additionally, my application will be inefficient since it is going to run for every change in battery state. Is there a way to make it run, only when the battery is full, which is my main purpose?
I am currently developing an android application where I need to setup persistent alarms that will fire on a specific date and time and show a notification. It works well but the system clears all alarms upon rebooting.
From other questions I know that if I create a BroadcastReceiver forBOOT_COMPLETED I can rearm canceled alarms. My question is: What information about those alarms I need to keep in order to rearm them when needed?
Some people say that I need to persist all the Intent extras and the fire datetime in order to recreate the PendingIntent upon rebooting.
Others say that if I only persist the requestCode for the PendingIntent, after reboot I can use this code to get the canceled PendingIntent and rearm the alarm, because when the device reboots the PendingIntent's are just canceled instead of deleted.
Which one is right way to do it?
PendingIntents will not persist after reboot, so to be safe just restart your alarms in the BroadcastReceiver with all the intent extras that you make when you first initialized the alarm, and keep the requestcode the same.
I want to create an Alarm for Android.
I've used so far an AlarmManager that sets on the given time and day.
And onReceive(), I have a NotificationManager that warns for a specific event.
My problem is that if the event is set for a week or month later, I may restart my device and my alarm will be lost.
Can someone explain me how can I save this alarm and restore it after reboot?
you need to use a broadcast receiver that listens for the broadcast
android.intent.action.BOOT_COMPLETED
then restart your alarms from your broadcast receiver
I have a broadcast receiver for receiving the 'BOOT_COMPLETED' event. What I want to do in the OnReceive method is to re-schedule an alarm whose time is pre-defined by the user. How would I go about getting the alarm time that the user entered into the application? I tried looking at SharedPreferences but these don't seem to be accessible out-with Activity classes. Does anyone have any ideas on how I could go about getting this information?
You don't have to set the alarm at boot time. You can use AlarmManager to handle that for you. You can even set repeating alarms with it.
I'm trying to figure out if a system event broadcast is broadcasted
using ordered broadcast or normal..
The event is EVENT_REMINDER and in the source for the calendarprovider
i noticed it sets up a alarmmanager to sent the broadcast.
I can't find how the alarmmanager sends it.
My guess would be as a normal broadcast ,
But while i was trying some things i noticed i could delay the
system's notification (tried up to 10 sec) by building a sleep in my
broadcastreceiver.
This would indicate that they are handled ordered , and the systems
receiver is only called when mine has finished.
But can i be sure of this behavour?? (in all 1.5> sdk versions)
the docs state that in some cases normal broadcasts are also handled
ordered..due to spare of resources.
All thoughts on this are welcome....
Thanks , arnold
You can check the isOrderedBroadcast flag in the BroadcastReceiver.