I want to make something in a specific time period. For instance, I'll show a message in my application from 6pm to 8pm everyday. I'll do it in a service. Which code should I use and could you please give a sample? Thanks in advance...
I would look into using Android Notifications along with AlarmManager to handle the repeating.
Particularly setRepeating in AlarmManager to call the service and creating the actual Notification in the onReceive of your service.
You should use an AlarmManager
http://developer.android.com/reference/android/app/AlarmManager.html
you can find tons of examples online
Related
Can anyone tell me how to do a service that will generate a notification every day at a certain time (for example at noon)?
Or rather, do I need a service to do this?
Naturally I am talking about Android programming
In Android, You can implement with options:
AlarmManager
Schedule job
Server schedule job
Or something other options,...
Hi I need to set AlarmManager to run a reminder for me to take medication. I need to repeat it by custom amount of days and custom amount of times to take in the day.
So is there an efficient way to set the AlarmManager or CommonsWare's Implementation of the AlarmManager to remind me "twice a day starting at 9AM for the next 5 days" to remind me to take medication? Pls advice and tnx in advance for any constructive help in sample code and in relevant tutorials.
I haven't looked into Mark's AlarmManager implementation, but there is no way, in general, to get the bare AlarmManager to do what you are trying to do. You can schedule a single alarm, at a specific time, or a repeating alarm, that repeats at fixed intervals. If you want something that handles complex schedules like the one you describe, you'll have to write or find code that does it.
You want to use a PendingIntent with the AlarmManager. The idea is to schedule the pendingIntent with the alarmManager, have that trigger an intentService or broadcast, setup another pendingIntent with the alarmManager for the next desired event. You want to keep in mind that you'll need the BOOT_RECEIVED permission in case the user reboots their device. I have complex scheduling in Audio Control and this is exactly what I do.
Here is a pretty decent tutorial of what I mean:
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
You need to schedule an alarm to the next time you want to take the medicine - according to your algorithm (for example if its twice a day, and you got to the pending intent callback for the first time today, then schedule the next alarm to start after [6,7,8,9,10...] hours).
You will need to save both last time of the alarm launch and the user settings in shared prefs/file/DB.
You need to handle process down (android killed it or the device was rebooted). In the case of device reboot you should use the boot receiver to start your service, but you need to remember that from android 3.1 the user has to use at least one time the GUI in order for you to intercept the boot completed receiver. The boot completed receiver should look when was the last time that the alarm launched, and according to the user settings set the next alarm launch.
In the case of android killed your service, you will need to make research, i can't help here.
see example
I want to start my application everyday at a particular fixed time.i tried the link
Start app at a specific time but didn't worked.
By use of AlarmManager you can declare the time. And use of BroadcastReceiver you can start the application at the time of reached. I suggest you to try Commonsware example application. There you should modify the code by yourself with your needs.
Hope this helps you lot.
You should achieve it using a service with AlarmManger for that.
First when user install your application then you should start Service then service start AlarmManager content a pendingIndent this pending intent fire on you specific time mention you in AlarmManager
I want to replace the Thread.sleep(time) calls in my code with alarms set with AlarmManager. How can I return to the proceeding code after the alarm has elapsed. It looks like I use a PendingIntent but how to I point it back to the proceeding code after the time has elapsed?.
Well, it depends on what values you have for time. If you are sleeping for 100ms, you definitely don't want to use AlarmManager, as piotrpo suggests. However, if you are sleeping for several minutes, then AlarmManager is appropriate.
AlarmManger and PendingIntents impose big overheads, so be careful with using them.
Alarm manager is not designed to something like that. .sleep() is also not good. Please read about Timer and Handler classes.
what do you prefer for making a notification on specific date and time... in this documentation I can't find where to define date and time...
However is there any other way to handle this notifications?
Check the AlarmManager class it can help you scheduling events.