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.
Related
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 have an application that will create a persistent notification but I need to update this notification each day so is there a way to create a time listener or how to do it otherwise but I need this update to be performed even if the activity is not started, so even if the user did not open the application, I saw this page but it's just for an app that is still in its started state once it's in its pause state... it won't work anymore... any idea?
The Best way is to use AlarmManager see this link http://www.techrepublic.com/blog/android-app-builder/use-androids-alarmmanager-to-schedule-an-event/
Otherwise you will need to write service to keep track on time.
Good luck
You could use an AlarmManager.
http://developer.android.com/reference/android/app/AlarmManager.html
Set up the alarm to trigger at the time you want to handle your event.
When you complete your event code, set up a new Alarm before you exit.
Note that alarms do not survive a reboot so you would need to set them up again at device boot (if required)
I'am trying to find a way to use an NotificationCompat.Builder.addAction(...) to call some code to stop a repeating alarm without starting an activity... but as i understood so far the only thing I can do with this is start intents with Activities.
There's any way I can stop the alarm without leaving the notification?
You can add request to alarm manager by calling set() function with PendingIntent as parameter. You just need to call cancel() with same PendingIntent, in order to stop it; though I note very clear about your notification question.
I have a repeating AlarmManager that calls a Service to perform some background updates. I read somewhere that I need to cancel the AlarmManager in the service's onDestroy, but this doesn't seem to make sense to me. It seems to be me you'd only want to cancel the Alarm if you don't want it to fire again.
You might want to provide a link, or a more detailed explanation/argument. You might want to
cancel background threads onDestroy(), but alarms are independent of the activity, and will fire (and create a process if necessary) even if your activity is not running. Once you cancel it, it will be gone, so you will not get those updates. You should only cancel if you don't need them anymore, e.g., an preferences option to cancel automatic updates to save battery, etc.
I need help in my android app development.
It goes something like this,
I will be having two separate applications (2 projects). In one application, i have to start a repeating alarm and in the other application i have to cancel the same alarm that was started in the first application.
The Android documentation says, the same pending intent and the intent object that was used to start the alarm
should be used the cancel the alarm.
So in this scenario, the pending intent and the intent object that was used to start the alarm will belong to application1 so i cannot used the same objects in application2
How do I proceed?
In summary -
The problem is, I need to start a repeating alarm from one application and i have to cancel the same alarm from another application.
Can this be done. If so, How?
Thanks in advance.
ifreeman
It is not that straightforward. Only original activity can cancel the alarm.
So I think you can configure a custom broadcast. When the second activity needs to cancel an alarm it will send this broadcast. The first activity will be listening to the broadcast and cancel the appropriate alarm on receiving it.
I guess you can do. Alarms are considered as same if intents passed to them via pending intent are same. filterEquals method of Intent class defins if intents are same or not. If intents are same then alarms are same so u can cancel that alarm. Check once.