Getting Saved Data from Broadcast Receiver (BOOT_COMPLETED alarm) - android

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.

Related

How to get time at which notification is to be fired in Service before notify()

I want to access the time in milliseconds at which the notification is scheduled while showing notification. How can I get it in Service? Alarm is scheduled in Activity. Based on that time, notification is to be altered. How can I achieve this?
Thanks in advance.
While scheduling the alarm using pending intent you can add a key-value pair to the pending intent. Once you get the alarm in your broadcast receiver you would be able to retrieve the value and add logic to display the notification. Hope it helps! And yes don't forget to reschedule your alarms on events of phone being restarted etc..
Step #1: Have your activity save the time it schedules the Notification for in a file, such as a SharedPreferences.
Step #2: Have your service read the time from that file.
You can optimize this a bit with an in-memory cache, but since I do not know your app, I have to assume that your process might be terminated between steps #1 and #2, which is why a file is important.

Restoring Alarms set with AlarmManager after device reboot

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

Android: What is the best way to restore alarms after reboot?

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?

AlarmService onBoot with interval based on variable

Alright so I'm working on a project where I want the app to check the internet for updates. I also want the user to be able to customize how often it makes these checks.
I'm kinda struggling with how I go about doing this.
I'm thinking I have a BroadcastReceiver check for the Intent.ACTION_BOOT_COMPLETED, and then start the AlarmService based upon a variable already set somewhere.
But what happens if the user wants to update the interval? How do I stop the old AlarmService and replace it with the new one?
Also how do I have my AlarmService run a "background update" portion in my app without actually running any of the activities?
EDIT: Also can someone advise if I'm using the AlarmService correctly? I'd want to check for updates fairly frequently, should I be using some other method? I'd check between 1-5 minutes.
I do a similar thing in my app. I basically have two services: one that sets the alarm and the other that does the actual donwloading.
When your BroadcastReceiver get's the intent ACTION_BOOT_COMPLETED you start your alarmService which sends an broadcast to start the downloadService.
So your BroadcastReceiver does both things: starts the alarmServcie if intent == BOOT_COMPLETED else it starts the downloadservice.
Also if the user changes the periods in which he wants those updates to be done you store the chosen value in the preferences, start the alarmservice and retrieve the values to set up a new period, e. g. 1 hour, 2 hours
Edit: Here is the info some info from the docs about pending Intents and how to cancel them. http://developer.android.com/reference/android/app/PendingIntent.html
When the user changes the interval you should first cancel your old alarmservice and then set it again.

Cancel AlarmManager which was set in Service

folks! I have Service, which checks in onStartCommand() whether auto update was set in user preferences and sets AlarmManager update time if needed. So, I want to acomplish following: consider that AlarmManager is alread set, and user turns auto update off, I want to cancel the alarm. The only idea I have is to broadcast custom intent to service that preferences were changed. Is there another way to do it?
UPD or I just need to call stopService()?
A broadcast is definitely the best way to communicate between an activity/widget and a service.

Categories

Resources