I am using AlarmManager to trigger a regular action of my app that should run whenever the device is on. Following the documentation I am receiving an intent of type android.intent.action.BOOT_COMPLETED to start the alarm whenever the device starts.
I am aware that the user needs to start the app before it can receive this intent as explained here and in many question on StackOverflow.
But when the user starts the app for the first time want to immediately set the alarm and not wait until the device reboots for the first time.
How can I trigger my alarm setup cleanly when the app is launched for the first time in addition to on BOOT_COMPLETED?
I guess could call the alarm setup in my launcher activity but this seems to bloat the Activity code and I don't want to execute it if the alarms have been setup at BOOT_COMPLETED already.
Related
I have made an alarm clock app that uses AlarmManager to place alarms. There is a broadcast receiver that receives the alarm broadcast, and then starts a foreground service.
Consider this situation: I do a fresh installation of my app on a device and add an alarm. I let a few seconds pass by, open a terminal window, and type the following command: adb shell dumpsys alarm. In the list, I can find the alarm registered by my app.
Now, I close the app (clear it from recents), make sure the alarm process is dead from Android Studio logcat, and again type the above command. I cannot find my alarm in the list! The alarm was there just a few seconds back, and now it is not there.
So I open the app again, delete this alarm, and add another alarm. With dumpsys, I can find my alarm in the list. I close my app and again execute dumpsys, only to find that this time, my alarm is there in the list. When the time arrives, the foreground service starts properly.
Why is the alarm getting cancelled after I close the app in case of a fresh installation?
In my app I want to schedule an Alarm associated with Activity A. When the alarm fires, what should happen depends on the circumstances:
1. if the app is not opened at that time, then display a notification in the notification bar and also start the activity A the next time the app is started
3. if the app is active, start Activity A
Activity A is not a background activity - it requires user interaction.
Ideally, the same behavior would work with more than 1 alarm - i.e. if 2 alarms fired while the app was not in use, two activities would be queued to start once the user starts the app.
I read the Android Docs on AlarmManager, PendingIntents and Notification - I know how to schedule an alarm which fires a notification, but I don't know at all how I would go about
- the "queueing" of activities after an alarm fires
- the "conditional behavior" when an alarm fires (based on whether app is running or not).
It seems like plenty of apps would need this kind of behavior. I hope someone can point me in the right direction.
Found a solution in the "Android Programming: The Big Nerd Ranch Guide" book (Hardy & Phillips). The solution is to use Intent Service that handles the AlarmManager intents, and sends an ordered broadcast. The broadcast is either received by an Activity, or - outside of the app lifetime - by another service, which then sends a notification.
I have developed an alert reminder type application, so when alarm is called my broadcast receiver call one activity and fire one notification and also start one service, it working good,
But when I working in other application some time the my service and notification is fired but activity is not open. it happen sometime not all.
So, how can I give the priority to my activity so it can open always if we working on other application, just like in call receiver screen.
I think you cannot bring your app to top when other other apps are in front. The call receiver screen is system window which has special privileges. You cannot have the same for your app.
As a alternative you can fire a notification to status bar.
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.
I'm writing code to display notifications to the user at specific times (just like the Google Calendar app).
I hence created :
a BroadcastReceiver that listens to BOOT_COMPLETED, upon reception it sets an alarm in one minute in order to not overload the device when it is still loading stuff;
a BroadcastReceiver that listens to alarms: the first set one minute after BOOT_COMPLETED, and the next at the next appointment (like in Google Calendar)
So, typically:
BOOT_COMPLETED => launch alarm with a one minute delay
One minute later => the Receiver sets another alarm for the next appointment
Several minutes/hours/days later, the alarm goes off => the Receiver displays a status bar notification
Which means that the status bar notifications are launched from the BroadcastReceiver.
I have read in the doc that they should be launched from Activities or Services : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Basics
I'm asking for best practice here. Should I create a Service that will be launched by the BroadcastReceiver, and which only purpose will be to launch the status bar notification? My code is working, I just want to create clean code as suggested by Google.
You can add a Notification from a BroadcastReceiver, AFAIK. That should be fairly fast. If StrictMode complains about it, then it might be worth worrying about -- otherwise, you should be OK.