BroadcastReceiver and AlarmManager at Device Boot - android

i've got a Activity in which an alarm is started at a time which can be choosen by the user; the time is stored in a database.
Than a BroadcastReceiver is called which fires a notification at the specific time.
So everything is working, but now i want to start the alarms also when the device is rebooted and i don't know how exactly i can do this.
According to this site Alarm Notification i should implement an other BroadcastReceiver which starts when the device is booted and fires the alarms like i do in my other BroadCastReceiver.
But on all other sites, they advice to implement a BroadcastReceiver which starts an extra service and than fires the alarms.
Also i wonder if it's possible that the BroadcastReceiver which could be started when the device is booted does have access to my database in which the date and time of the alarms to be fired are stored.
Thanks everybody

Yes, as it's part of the same application it has access to the database. So you could receive notification of device boot then set your alarms up using the time in the database, or fire the alarms. Or both.
Hope this helps.

Related

Offline reminder notification app (Google Keep like) for Android 11 target. How to receive notifications when app is closed?

I searched a lot, but couldn't find nothing clear. Every solution seems outdated. In my app, I need this simple task: the user can enable a daily reminder and select a time in the Time Picker.
Everyday in the selected hour the user should receive a simple notification. I only need to read the local database (Room, SQLite) and then show a notification.
I'm using AlarmManager, because the user should receive the notification in the exact time he selected.
Everthing is working fine when the app is open or minimized. When I close the app in the recently used apps (swipe up) I don't receive the notification.
I have a class that creates the alarm with this calling:
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
The pendingIntent opens a NotificationReceiver extends BroadcastReceiver class. In the onReceive method I read the db and show the notification. I also tried starting a service in the onReceive method (IntentService or Service) but they work exactly as just using the broadcast receiver.
I put the receiver in the AndroidManifest. Everything is working except when I close the app or restart the system (I registred the BOOT_COMPLETED in the manifest and I have another broadcast receiver that configure the alarm again. If I open the app and keep it open before the notification time, then I receive it. But if the app is still closed, I don't receive it. So I thik the BOOT_COMPLETED is working fine).
The other solutions doesn't seem to work. What can I do to have it working on Android 11 (Pixel 3 device)?
One example of what I need is the Google Keep app, but mine is simpler. I don't see any notification dot telling that the Google Keep is running in background. Still, I get the reminders on time. How the Google Keep works? It's a service always running (and killing the battery)? It uses AlarmManager and BroadcastReceiver? WorkManager? Even if a close the app or restart the phone, I always get the notifications. How can I achieve it on my app?

android - how to push notification (reminder) on a specific time?

I checked almost all questions in stack overflow and also in google documentation and still couldn't understand what to use exactly for my cases.
What i want is, user will select reminder date in the app and in that time app will send a notification even app is closed or phone restarted between remind set time and remind time.
So what do i need to use? Which classes do i need? Broadcast Receiver, AlarmManager these two is enough or what? a sample of code 20-30 lines would be nice =)
You will need both a few things.
Setup:
You need to set your app to listen for the phone starting by registering for this intent in your manifest.
Basically you when your user selects the time, you will need to:
Save that time in a file/db/shared prefs.
Set the alarm manager to wake up your app at the right time
When the alarm manager sends
you a broadcast, start a service to do the necessary work (broadcast
receivers have to complete their work in a short amount of time, so
it's better to start a service)
In your service, remove the time
from your file/db/shared prefs
If your app ever gets the broadcast that the phone was rebooted, then all of your alarms are lost. You need to reset them using the times you saved in the file/db/shared prefs.
Check Android Developers website for calender intents so you can use them to determine the time etc.
On your broadcast receiver pass the details or timing to the broadcast receiver. Once you do that use the alarm manager etc to check if the time is perfect and simply send a push notification when the time is correct.
I say this assuming that you know already about the calendar, alarm manager, and broadcast receivers intents. If you don't I recommend googling the following with Android Developers appended to it.
Use AlertManager.set to send notification on particular time.
For re-setting Alarm Notification, use
<receiver android:name=".AfterBootReceiver" android:label="AfterBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
and
#Override public void onReceive(Context context,Intent intent){
...
}
to reset AlarmManager.set after device re-booted.

Alarm in the AlarmManager get erased when process is killed

I'm a newbie in android so please bear with me.
My Main activity creates and alarm in the alarm manager which supposed to fire in specific time, my main Activity also create Broadcast receiver which suppose to receive the Intent that the alarm fired, everything is working good until Task manager killing my App.
I've check the PendingIntent list in the AlarmManager and verify that my alarm is getting erased from the Alarm Manager, I try to add service and register alarm from the service, I've red that maybe because my IntentFilter of the Broadcast receiver is defined in code and not in manifest it get killed after app process is killed, and I'm stuck on this issue for two weeks :-(, with big confuse, my design is wrong ?
Here is my needs:
That the alarm will be very reliable, even if app is killed or even if phone is restart.
Same goes to the broadcast receiver.
Thank you in advance,
If the user task-kills or force-stops your application, your alarms are unregistered. And, on Android 3.1+, nothing of your app will run again until the user manually launches one of your activities.
There is nothing that you can do about this, other than to do your best to write a high-quality application that the user will have no need or wish to force-stop.

How to notify a user with SMS or notification when needed

So in my app I'm looking to have the students class schedules and the times which they need to be in class.
When the app is turned off I still want the notification to pop up that their class is about to start (possibly vibrate).
This sort of functionality is very akin to an incoming text message or notification of something like an email. I was wondering how to implement that into an app?
You need a Service which can run when the app is not opened. You should also think about a BroadcastReceiver that listens to BOOT_COMPLETED
You will need to use the AlarmManager to set an alarm when you require the notification, possibly with the RTC_WAKEUP flag so the device will wake from a sleep. From your alarm receiver you will need to take a wake lock (if you used RTC_WAKEUP) and start a service that will use the NotificationManager to display a message to the user (very similar to the incoming SMS message).
As #WarrenFaith pointed out you will need to create a BOOT_COMPLETED receiver to re-establish the alarm after the phone is rebooted as they are not persistent.

how to get notification of Alarm which is passed in between switchoff to switched on mobile

Hi
I am developing a application in which i am using AlarmManager
Problem
When i set pending Intent using Alarm manager on perticular date and time its work fine
but suppose i set alarm time on date 30-05-2011 and time 10:00 AM and suppose current time is date 30-05-2011 and time 09:50 AM
and now after creating pending intent i switched off my device and after 10:01 AM i starts my device
at that time i expect notification for 10:00 AM alarm but i am not getting it
Any idea how i can get notification After switch on my mobile
Through AlarmManager, you can only wake up your device when it is sleeping.
To do that use
setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
or set(...)
with RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP
But it doesn't work with a device at off.
So you should consider having your alarms in a database, along with the last time your app was on, and count the alarms you missed since last time you were up.
Regards,
Stéphane
If you read the AlarmManager API doc page:
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.
As an alternative, you can register a broadcast receiver for the intent android.intent.action.BOOT_COMPLETED and check your SharedPreferences if you need to perform an action.
See this question for details about the broadcast: Trying to start a service on boot on Android

Categories

Resources