I am writing an app which needs to periodically check the server for new messages and notify the user. I have seen some examples using AlarmManager to hit a BroadcastReciever which seems like the right thing to do, but i cant seem to get it to work.
Can anyone show me a step by step tutorial for this sort of thing (repeating alarm which triggers some kind of background code that fires a Notification)?
TIA
Here is one complete example: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/
The pattern this example uses, and one that I've found that seems to work well, is to use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealBootReceiver.java
And then from the AlarmReceiver start an IntentService:
http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealAlarmReceiver.java
From your IntentService then make your network call to poll for data, or whatever you need to do. IntentService automatically puts your work in a background thread, it's very handy:
http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealService.java
Check the docs for these classes too, a lot of into in there.
The caveat with this example is that it does not deal with the wake lock gap (the excellent CommonsWare code does that if you need it), but it may give you some more ideas about how to potentially address the "poll using AlarmManager and Service" stuff.
UPDATE: the code is now here: https://github.com/charlieCollins/android-in-practice
Related
I have Android service to run and send email every 9 mins but after some cycles it quits. this app is also installed in my Android froyo but it works good for more than 2months now.. I can see the logs in logcat that the service quits but my problem is I cannot understand what does it mean.. Can someone help me with this? Thanks!! any help would be appreciated...
here is my source code:
https://gist.github.com/77a40ac93cd311acb56c
Logcat logs:
https://gist.github.com/dd3ab385d79253fac632
The point behind using AlarmManager is so that your service only needs to be in memory when it is doing actual work, and can go away in between AlarmManager events. You have managed to not do that, and therefore your code will not work reliably.
If you want to "send email every 9 mins", you should:
Move the BroadcastReceiver to be a public Java class, registered in the manifest via a <receiver> element, and remove the registerReceiver()/unregisterReceiver() stuff.
Switch your service to be an IntentService, so you get a background thread (which you need for your work, but your current code lacks) and so the service can automatically shut down when there is no more work to be done.
Add in the logic for the WakeLock that you will need since you are using a _WAKEUP-style alarm. You could combine this and the previous steps by switching to my WakefulIntentService, if you wish.
Deal with the case where the user reboots the device, if you want your alarms to continue after the reboot, such as by scheduling them again via an ACTION_BOOT_COMPLETED BroadcastReceiver.
In my app I would like that a certain method of mine (call it toSched) will run at a given time in the future (I'm using Timestamp for knowing when).
How can this be done, and is it possible to do it even if the phone is turned off (assume there is enough battery) ?
*I know I should use AlarmManager but I'm not sure how
You want to use the Android Alarm Manager. You do it by creating a PendingIntent (which is what you want to run) and then calling the set method. Checkout this stackoverflow post for more info.
What most people do is create a PendingIntent that is a broadcast. Then they create a broadcast receiver to receive that broadcast and do what ever is supposed to be done. Here's an example of "calling a method" to start an activity. But you can just put your own arbitrary code in the receiver to do what ever random task.
There's several ways of looking at this. One alternative (the one I think you're looking for) is discussed in this Stack Overflow thread:
How to set a timer in android
Another is to make sure your program gets invoked when you need it to:
AlarmManager
... or ...
cron
I currently have a service setup that emails a bunch of files. What I want to do is add a scheduling system setup so that at a certain time each night, that service runs (those emails are sent).
I thought maybe a Broadcast Receiver triggered by an AlarmManager would work, and it does except it only runs when the app is running. I read that Broadcast Receivers only run in the UI thread. I need this to work regardless if the app is running or not.
Im going to assume that what I need is a broadcast receiver to start [blank] to run in the background and when the AlarmManager sends an alarm that [blank] will start the service I already have setup.
If that is the correct procedure, what is [blank] ? If its not the correct procedure then what is ?
Thanks
You may want to run a RemoteService (http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html), and this article explains how to use the AlarmManager to start up a Service.
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
I actually made this change today, and my program is working better at work.
The RemoteService is so that the Service doesn't die when your Activity dies, basically.
Your procedure is correct,if you don't need an IPC ,then no necessary to implement a remote service.
I'm implementing an background process that will update information my app uses.
I only want this process to update say once a day, if the process gets data newer than what it had before I want to present the user with a notification, exactly like twitter/gmail does.
I want the update process to run automatically, even when the main app is not open.
Is a Service the best way to go? I've been reading quite a bit about this, I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
However I notice google run service for friendlocation and google+ services continuously on my nexus.
I've look into starting my service via the AlarmManager so its only started when required.
Some posts also suggest using the Handler class, I don't think this will work.
Just looking for the best practice here.
I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
Absolutely.
I've look into starting my service via the AlarmManager so its only started when required.
This is the correct answer.
If you only want your code to be invoked if the device is on, implement an IntentService, do your work in its onHandleIntent(), and have AlarmManager start up the service on your desired schedule.
If you want your code to force the phone to wake up, you can do that, but you will need to use a _WAKEUP-style alarm, and you will probably want to look at my WakefulIntentService, designed to handle this pattern.
I see all the examples of AlarmManager being set by an Activity.
My question is this:
If my application sets a recurring AlarmManager, does that persist even after the application that started is is closed and removed from memory?
If not, how do I start an AlarmManager at a lower level that is started by Android at boot up and if it ever fails or dies or throws an exception is restarted without the user having to do anything?
Lastly, if the action I wish for the BroadcastReceiver to undertake has no visual components, do I still have to create a separate Activity for it? In my case I want there to be a background uploader that wakes up and looks into a folder and if it sees files in that folder, sends them off to the server. I don't need any feedback to the user.
So, my ideal would be to have a magical, OS based AlarmManager that calls an IntentService which just handles the uploading, but I'm unclear on how to get such an AlarmManager running in the first place.
TIA
Yes, AFAIK the alarms "survive" and keeps getting triggered, even after the activity that registered them ends. But they don't survive a phone reboot.
If I understands your problem correctly, I think you can achieve what your looking for by creating a project with a broadcast receiver that listens for android.intent.action.BOOT_COMPLETED intents and then (re-)register a repeating alarm, which in turns starts a (Intent)Service to do the uploading.
You don't need an activity, but you probably would want one anyway, to let the user temporarily disable the upload mechanism by checking off a checkbox, or something. It would probably also be nice to let the user choose the frequency of your alarm, i.e. how often the service should be started and look for new files to upload. This would also be a good place to register your alarm for the first time.
I agree with Nicolai that you'd have 2 broadcast receivers in your application :
one that re-register the alarm on boot
one that starts your service when triggered by the alarm
You could still have an activity but it shouldn't be started by the alarm receiver (hence the service) : instead, maybe launch a notification as you start your service, with the user having the possibility to launch the activity from the expanded message of the notification.
maybe also think about setInexactRepeating (instead of setRepeating) for your alarm, as well as using a worker thread to handle the long uploads (in case the user wants to use your activity in the main thread at the same time).