How to program notifications like google keep? - android

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

Related

Create Reminder Notification for Android

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.

Starting a service at regular intervals in android

My aim is to run a background service every 6 hours in the android application.
What i have learnt from reading up is : I need to use alarmManager, setup alarms, receive those using broadcast receiver and then start my service in the onReceive method.
Now, i don't want to set the alarm for some particular time, as then all apps would send requests to the server at the same time. How do i get around this ? Also, most tutorials i read, registered the alarms in the onCreate of Activity. Wouldn't that lead to multiple registrations of the alarms, every time i start the app ? What would be the better place to do this.
You use
http://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME
or
http://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME_WAKEUP
which uses "time since boot" as a base, which is precisely for the case you're mentioning above.
Also there is no multiple registration of the alarm, as long as the provided PendingIntent is the same.

Is there any way, given user permission, for an android app to start on its own at a scheduled time in the future?

As the title suggests, is there any way to have an Android application be schedule to start on its own at some time in the future. For example, is it possible within the Android API for a developer to write an application that might initiate at 7am if previously scheduled by the user?
You can use the Alarm Manager.
This class provides access to the system alarm services. These allow
you to schedule your application to be run at some point in the
future.
http://developer.android.com/reference/android/app/AlarmManager.html

writing a background program that executes when the main application is closed in android

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

Notification when app not used for more than certain period

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.

Categories

Resources