I checked almost all questions in stack overflow and also in google documentation and still couldn't understand what to use exactly for my cases.
What i want is, user will select reminder date in the app and in that time app will send a notification even app is closed or phone restarted between remind set time and remind time.
So what do i need to use? Which classes do i need? Broadcast Receiver, AlarmManager these two is enough or what? a sample of code 20-30 lines would be nice =)
You will need both a few things.
Setup:
You need to set your app to listen for the phone starting by registering for this intent in your manifest.
Basically you when your user selects the time, you will need to:
Save that time in a file/db/shared prefs.
Set the alarm manager to wake up your app at the right time
When the alarm manager sends
you a broadcast, start a service to do the necessary work (broadcast
receivers have to complete their work in a short amount of time, so
it's better to start a service)
In your service, remove the time
from your file/db/shared prefs
If your app ever gets the broadcast that the phone was rebooted, then all of your alarms are lost. You need to reset them using the times you saved in the file/db/shared prefs.
Check Android Developers website for calender intents so you can use them to determine the time etc.
On your broadcast receiver pass the details or timing to the broadcast receiver. Once you do that use the alarm manager etc to check if the time is perfect and simply send a push notification when the time is correct.
I say this assuming that you know already about the calendar, alarm manager, and broadcast receivers intents. If you don't I recommend googling the following with Android Developers appended to it.
Use AlertManager.set to send notification on particular time.
For re-setting Alarm Notification, use
<receiver android:name=".AfterBootReceiver" android:label="AfterBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
and
#Override public void onReceive(Context context,Intent intent){
...
}
to reset AlarmManager.set after device re-booted.
Related
I need to integrate, in my android application, a calendar from which i can set new event.
I know that is possible call the android calendar (using intent), but what I need is slightly different, for this reason:
I need that when the time of the event is reached, some code is started, and no default notification occurs! In other words I want create a custom notification when alarm of event goes off. Maybe I need to use AlarmManager
How can I solve this problem?
If your problem is that you need to launch some kind of intent at a specified time then you can use AlarmManager. It allows you to schedule your application to be run at some point. When an alarm goes off, the Intent that had been registered for it is broadcast by the system.
For More information about AlarmManager, you can check this link : AlarmManager
I'm a newbie in android so please bear with me.
My Main activity creates and alarm in the alarm manager which supposed to fire in specific time, my main Activity also create Broadcast receiver which suppose to receive the Intent that the alarm fired, everything is working good until Task manager killing my App.
I've check the PendingIntent list in the AlarmManager and verify that my alarm is getting erased from the Alarm Manager, I try to add service and register alarm from the service, I've red that maybe because my IntentFilter of the Broadcast receiver is defined in code and not in manifest it get killed after app process is killed, and I'm stuck on this issue for two weeks :-(, with big confuse, my design is wrong ?
Here is my needs:
That the alarm will be very reliable, even if app is killed or even if phone is restart.
Same goes to the broadcast receiver.
Thank you in advance,
If the user task-kills or force-stops your application, your alarms are unregistered. And, on Android 3.1+, nothing of your app will run again until the user manually launches one of your activities.
There is nothing that you can do about this, other than to do your best to write a high-quality application that the user will have no need or wish to force-stop.
i've got a Activity in which an alarm is started at a time which can be choosen by the user; the time is stored in a database.
Than a BroadcastReceiver is called which fires a notification at the specific time.
So everything is working, but now i want to start the alarms also when the device is rebooted and i don't know how exactly i can do this.
According to this site Alarm Notification i should implement an other BroadcastReceiver which starts when the device is booted and fires the alarms like i do in my other BroadCastReceiver.
But on all other sites, they advice to implement a BroadcastReceiver which starts an extra service and than fires the alarms.
Also i wonder if it's possible that the BroadcastReceiver which could be started when the device is booted does have access to my database in which the date and time of the alarms to be fired are stored.
Thanks everybody
Yes, as it's part of the same application it has access to the database. So you could receive notification of device boot then set your alarms up using the time in the database, or fire the alarms. Or both.
Hope this helps.
So in my app I'm looking to have the students class schedules and the times which they need to be in class.
When the app is turned off I still want the notification to pop up that their class is about to start (possibly vibrate).
This sort of functionality is very akin to an incoming text message or notification of something like an email. I was wondering how to implement that into an app?
You need a Service which can run when the app is not opened. You should also think about a BroadcastReceiver that listens to BOOT_COMPLETED
You will need to use the AlarmManager to set an alarm when you require the notification, possibly with the RTC_WAKEUP flag so the device will wake from a sleep. From your alarm receiver you will need to take a wake lock (if you used RTC_WAKEUP) and start a service that will use the NotificationManager to display a message to the user (very similar to the incoming SMS message).
As #WarrenFaith pointed out you will need to create a BOOT_COMPLETED receiver to re-establish the alarm after the phone is rebooted as they are not persistent.
I have Some Alarms set via AlarmManager to do some periodic jobs.
I set/reset them when user opens the app for the first time & on every boot_complete event.
But when the app is reinstalled via ADB then my alarms do not fire anymore. looks like the OS deletes the Alarms on reinstall of the app. I assume this will happen if the user updates the app from the market also.
If I can receive a broadcast or some sort of callback in the event of reinstall/upgrade etc of my application, I can set the alarms again. but i don't know if it is possible or how?
Can someone please help me out.
Yes, this is possible.
You can create a broadcast receiver that listens for any PACKAGE_* events the system sends, but you won't receive them for your own application except for when your application is being upgraded — you'll get PACKAGE_REMOVED followed soon after by PACKAGE_REPLACED.