in my application user can set an alarm and my application is supposed to send an sms if the user doesn't respond the alarm in time ( let's say alarm goes off at 10:00 pm and user didn't reach the phone to turn it off till 10:15 than the phone will send an sms). I have looked the alarm manager but couldn't figure out how to do that.
you can achieve this by using broadcast receiver. when your user set the alarm on the activity, you can trigger it to send broadcast to the broadcast receiver. you can either execute your task on the broadcast receiver or you can start a service. for reference, check the links below
http://javatechig.com/android/repeat-alarm-example-in-android
https://blog.nraboy.com/2014/10/use-broadcast-receiver-background-services-android/
Additionally, make sure that you're app auto start after bootup to make sure that the alarm is not cleared when the phone turns off.
For reference, http://karanbalkar.com/2014/01/autostart-application-at-bootup-in-android/
Related
Do we have any kind of broadcast message when alarm goes off in Android? I couldn't find one. When the alarm goes off, I'd like to start my apps.
EDIT: It's the build-in alarm.
How do I make a broadcast receiver which runs always, from the very start of the device? Is this possible without starting the application where it is declared?
If not I guess I would have to start my application when the device starts. But this probably adds to much overhead and it's unwanted.
I want this always runnning broadcast receiver in order to listen always for c2dm notifications. These should notify the user that there are new messages in the application.
If you add the BroadcastReceiver to your Manifest with an Intent Filter to listen for a specific intent, the Receiver will be active upon install.
what do you mean "run always" ?
if you need something to be alive for a long time , and from the OS bootup , you need to :
let the app to be installed only on the internal storage (otherwise it won't work).
set the broadcast receiver to listen to the boot intent .
upon receiving the boot intent, start a service and there listen to the intent you wish to listen to.
I am doing a project in which i have to send a message at a particular time.that function is already in many mobile apps.But what i want is even if the mobile is switch off,i want to turn it on programatically at a particular time and send the message .
Use AlarmManager with an RTC_WAKEUP alarm to get control at your specified time. Have the AlarmManager trigger a BroadcastReceiver via the PendingIntent you use when setting up the alarm in AlarmManager. Have the BroadcastReceiver send the message.
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.