I am a newbie to android , and I am designing an app that reminds event , I am not aware of how to get started to achieve this .
I stored the Date Month Year of the event in the database , the thing I need is
Run a background service that looks for the event to occur
push notification based on time set by the user , also days set that is 1 days before , 15 days before
Please help me how to achieve this
Use the AlarmManager.
You can register a pending intent to be triggered at any time, or periodically. You can then make the pending intent trigger a broadcast or a service or just a notification, if I remember correctly. Just check this or google for Notification / AlarmManager.
This way is more power efficient than just run a background service that checks periodically if it needs to notify the user.
Related
I know how to create scheduled notification in Android by using alarm service but what I want to do now is to create notification in more frequent way.
For example, I want to push notification for 8 hours at the interval of 20 mins. In this case, is it efficient to use alarm service or timertask will be the better choice?
No matter which method, I wish to able to cancel it in the halfway. Thanks.
Timertask starts new thread and works in it. So if your app will work in background and your app will be closed by android, you won't receive any notification. AlarmManager provides access to the system alarm services. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. (link). So it will start your app even if it was closed. And you have to understand how you app will work with this notifications. If it works only while user works in app, you can use timertask, but if it has to work in background(for example you will receive notification even if user doesn't work with a phone/tablet), it will be better use alarmanager. Hope it helps.
I am building a game and now I have to send user scores on every first of the month to the server .
I have multiple options like
Timer
ScheduledThreadPoolExecutor
Service
BroadcastReciever with AlarmManager.
but not sure which one to use.
for the following reasons:
I don't think Alarm or Timer will be the best as it doesn't make sense to have an alarm after every month as user may can install the app at any date.
besides that I can't even check the date from user device as what if the user has wrong time set on device?
And what if the user doesn't have the internet connection available at first of the month or what if he didn't launch the app on the first.
I know we can use service for this problem but again is it feasible to always run the service in background which only requires once a month?
I checked other SO post too like this Scheduling recurring task in Android
but its not same as my case as I only have to make the calls once in a month.
Thanks.
use
AlarmManager is your best bet.
Your Issue:-
while scheduling an alarm check the current date and schedule the alarm accodingly EX:- if today is 17th schedule alarm after 14 day.
Regarding getting the current date put the current date on server and get it from there
in my opinion you should write a service and execute timer in your service which will be executed after every 24 hours and than if you don't want to get date and time from user device you can use google API or any other API for getting time and than decide if app needs to update score or not.
Actually I need to trigger an action(Intent) for the calendar events start time.
I had used the Calendar database to read the events title,start time and end time.
By passing the first start time to the alarm manager, I was able to trigger my action only once. My main problem is how to continue this process by passing different time intervals to the Alarm Manager.
Or is there any better solution to notify the action(Intent) ??
Thanks in Advance
It appears there is no intent to act on using a broadcast receiver.
You can however, read the event instances manually and schedule background tasks using the AlarmManager.
Credits to this answer on a similar question
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
Is there a way to send a notification to the user that app has been downloaded but a certain task from the app is not yet complete even after certain period - say a month. One way is a background service which should come alive every month in this case, check the app state (in sharedprefs) and then send a notification. Is there some other easier way in Android without writing custom service.
Here's how I would do it. Schedule an alarm using the AlarmManager to go off a month from today. That alarm can trigger some code inside of a Receiver or otherwise to check whether the said event has occured. If it hasn't, you can then show a Dialog or whatever.
In order to wake up your app after some amount of time (in your example a month) you're going to have to set an alarm. You can use AlarmManager for that. If all you're going to do is check SharedPreferences, you can do that in a broadcast receiver. You can send your notification there.