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.
Related
I am creating a notification app, which will alert user after they set a reminder notification. My current implementation logic is:
Create a Service, which starts running in background when user opens their app.
In onCreate() method of service, I am implementing a Timer task which will repeat after 5000ms interval and will call a method, which will check all reminders set by user and notify user if any reminder is set for current time.
I use broadcast receiver to run the service on Boot_Completed event, if in case user turns off their phone.
This implementation is working good, I have faced no issues with it, but my concern is that is this a good practice? Keeping in mind that service running and checking every 5 secs will consume battery. Also if user turns on Stamina Mode, Power saving mode or any such mode, will OS kill my service. Is there anything I can do to give priority to my Service not to be killed.
If there is any other more efficient way to implement this sort of task, I want to implement that in my project.
Looking forward for suggestions.
Much Appreciated.
best approach is wakeful intent service with alarm receiver as mentioned here
https://github.com/commonsguy/cwac-wakeful
all good but use AlarmManager.setRepeating() as timer. the intent come even if your app killed.
I need notifications like Google keep which run at a specified time even when app is not running. I mean reminder notifications, which remind according to the time set. There are lot of applications which do this like ColorNote.
I know how to create a notification. I don't know how to schedule it a later time , even when app is not running.
You need to create a Service to keep going while your app is not running, and set an alarm using AlarmManager to schedule your action.
Read about the right way to do repeating alarms here:
https://developer.android.com/training/scheduling/alarms.html
And about Services here:
http://developer.android.com/guide/components/services.html
I've developed an app to schedule multiple local notifications to remind users to do something. Every month in the current year there should an notification be raised.
These local notifications are scheduled using an "AlarmManager". A notification is created and raised in the OnRetrieve of a "BroadcastReceiver".
It works all fine until the app is terminated (by user) or the device is rebooted.
After some research I found the solution to reschedule the alarm / local notifications if the device is rebooted => using a BroadcastReceiver with "ActionBootCompleted" as intent filter and then reschedule the notifications in the "OnReceive".
Unfortunately I can't find a decent solution to reschedule the alarm / local notifications if the app is terminated.
What is the best approach for this case?
Try to run this as a background service. When the user opens up the app for the first time, call the service OnCreate(). Make sure the service is START_STICKY so it cannot be stopped unless you explicitly tell it to. Then place your AlarmManagers inside the service.
I am writing an android application, which communicates with server when the user logs in to the application. Now if the user closes the application without logging out, I want to query the server every 15 minutes, to see whether the particular user has received any updates. If yes then, I want to push a notification, on clicking which the user goes straight into the applications activity which shows the update.
How can this be implemented in android? Is it possible?
Can anyone suggest solutions using timer? And please remember this background program should run only when the actual application is closed.
You can use Services for this purpose. Take look at this:
http://developer.android.com/guide/topics/fundamentals/services.html
http://marakana.com/forums/android/examples/60.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Yes, this is possible.
I would do the following:
Use AlarmManager with setRepeating. This will get you your 15 minute interval.
In setRepeating, pass in a PendingIntent for an IntentService subclass
In your IntentService subclass, in handleIntent, query your server then create a Notification like documented at http://developer.android.com/guide/topics/ui/notifiers/notifications.html
The Notification will contain another PendingIntent which will bring the user back to your app. Make sure to specify the Activity that contains the UI that is relevant for that update.
You can learn more about IntentServices in the Services Guide at http://developer.android.com/guide/topics/fundamentals/services.html
You can learn more about AlarmManager at http://developer.android.com/reference/android/app/AlarmManager.html
I am creating an app which needs to retrieve a textfile from a webserver.
I have the manual retrieving done, just that I am thinking of adding a service to make the service check for the textfile like every 5 minutes and then send a notification to the user when a textfile is detected.
Is it ok to use a service here or do I have to use other methods?
Because the service will be kept running.
The best way of doing this is setting a repeating alarm using AlarmManager, a PendingIntent and a BroadcastReceiver as well as a Service. That way you don't have the service running all the time.
So the AlarmManager fires off the PendingIntent which is then picked up by the BroadcastReceiver which then starts your Service to perform the task in the background using a Thread or AsyncTask etc. Also look at IntentService which runs a Looper to process an Intent and then shuts itself down.
This is a great use of a service. Just make sure to allow the user to enable or disable service component. Also, try to shut it down when its not necessary (when the user cannot react to it).