I want to create a broadcast receiver that will get notified when an alarm is started.
For example I set the alarm to 10 am and I go to sleep, then when the alarm is fired I want to have an receiver that will be notified.
Is it possible to do this ?, is there any intent that is fired on alarm start ?
For setting alarm use Alarm-manager class. You can set alarm using pending intent and using calendar you can set the time. Check the following code, in this AlarmReceiever is broadcast receiver which receives intent from pending intent at specific time that you can set in set method as second parameter .
Intent alaram=new Intent(FirstActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(FirstActivity.this, 0, alaram,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
//cal.setTimeInMillis(System.currentTimeMillis());
cant you use pendingIntent too!! just saying
It appears that there is no standart alarm used by alarm applications. So either you have to look up used alarm in your logs and use it ( this will be alarm application dependent, and
surely not portable ) or just use alarm manager and schedule your own alarm
Related
I have a situation in my application. I am calling a service using an alarm. The alarm wakes up every 5 min and calls the service.
But it may so happen that the user might close my app and I am allowing the functionality of the service to work even if the user is not using my app.
To stop the service there are two ways either the user comes back to the app and presses a button which will cancel the alarm OR the second way is say after x time I want to stop the service i.e cancel the alarm from a broadcast receiver.
Now how can I do the second way ? When I tried to get reference of the AlarmManager it is giving me error of Null Pointer. (I am accessing this alarm manager from a broadcast receiver)
Can anyone give me suggestion on how to cancel repeating alarms from outside the activity context ?
Thanks :)
You can stop your service like this
context.stopService(new Intent(context, YourService.class))
Also in order to cancel the alarm you can do this
Intent intent = new Intent(this, YourClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
I hope this might help you
In your BroadcastReceiver try this:
public void onReceive(Context context, Intent intent) {
context.stopService(new Intent(context, YourService.class));
}
How do I periodically broadcast an event after user has installed my application.
I have implemented a broadcast receiver which will trigger a task onReceive. Now I need to periodically broadcast this event, so that the task will be executed periodically.
I know I need to use AlarmManager.
The code is somewhat like this.
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, 0, 5*1000, pendingIntent);
Q1) Where do I place this code if I do not have any Activity? Do I implement this as a service?
Q2) What should be the intent-filter be for my manifest in such a "self created event"?
PS: Actually my current broadcast receiver is waiting for the connectivity change event. The task is to periodically attempt/try to access the internet. But I cannot solely rely on connectivity change to trigger this task. That's why I need a timer which will either fire this event or trigger the method in onReceive().
I know in this case my intent-filter will be "android.net.conn.CONNECTIVITY_CHANGE"
Q1) Where do I place this code if I do not have any Activity?
You need to implement an activity. Nothing of your application will run on Android 3.1+ until the user has launched your activity.
So, you run this code:
The first time the user runs your activity
On a reboot (via a BOOT_COMPLETED BroadcastReceiver)
In the future when the user runs your activity, if you have determined that no broadcasts have gone out in too long, because the user force-stopped your application
Also, get rid of getApplicationContext(), as you should not need it here.
Q2) What should be the intent-filter be for my manifest in such a "self created event"?
You do not need one, as your Intent identifies the component.
I am writing an application which needs a homewidget showing the current date. For that I have used the alarm manager registering as follows:-
Intent intent = new Intent(HomeScreenWidgetProvider.ACTION_UPDATE_DATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetId);
intent.setData(getUriData(appWidgetId));
PendingIntent datePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarms.cancel(datePendingIntent);
**alarms.setRepeating(AlarmManager.RTC, getTimeForMidnight(), AlarmManager.INTERVAL_DAY, datePendingIntent);**
As you see, the above code registers for notifications first for the next 12.00AM with an interval of a day from there on. I update the date in my widget when I get a notification from alarm manager.
There is one big problem though. I don't get the alarm manager events when the date changes (at 12.00AM). And so the date does not change in my home screen widget.
The above code works fine in the emulator but not on the device. I am using Samsung Galaxy S 19000 for (real time) testing.
Is there a problem with use of alarm manager? Or is there an alternate way of receiving date change notifications?
I think in setRepeating() method, you should set the first parameter as AlarmManager.RTC_WAKEUP. If you use AlarmManager.RTC, the alarm event won't be triggered when the device is asleep. Please see the SDK document about AlarmManager for more details.
Also, you can register a BroadcastReciever listening to ACTION_DATE_CHANGED, by which you can receive date change event.
Hope it helps.
I want to set notification for an event which will repeat every day. So the notification should come everyday at event time. How to set any notification in NotificationManager so that it repeats after certain period of time.
If you are using AlarmManager class, it's more easier that to setup a service.
alarmManager class has a setRepeating method that repeats your alarm call at given interval after given time.
Like..
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent AlarmIntent = new Intent(CONTEXT, RECEIVERCLASS.class);
ID,AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,YOURCALENDAR.getTimeInMillis(), AlarmManager.INTERVAL_DAY, Sender);
In the setRepeating argument, you can set the YOURCALENDAR member to the time you want......
You kinda need a Service
for this, if I got your question right
for this u have 2 made one service class that notify your event.
when any event occurs just call start notification on event. if u not get proper idea that comment on this ans. i'll explain in detail.
I want to set an alarm in my application which will be triggered each day. According to the doc, I have to set a one-time alarm, and in the BroadcastReceiver which will receive the alarm signal, reset the alarm for the day after.
Is that correct ?
My BroadcastReceiver handles well the wakelock and launch a service which releases this wakelock. Everything works fine here.
However I have problems. In my application there is a checkbox which is checked when alarm is up. To know if my alarm is up, I use the following condition :
Intent intent = new Intent( context, AlarmReceiver.class );
boolean alarmUp = (
PendingIntent.getBroadcast( context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null)
But this doesn't seem to work very well, is that a good way to know if an alarm is up ?
Thanks in advance
For the first part of your question, you could just use a repeating alarm, or schedule a new alarm whenever one fires like you are doing. Either way works.
You may also want to setup a broadcast receiver that receives ACTION_BOOT_COMPLETED so you can reschedule your alarms when the phone reboots.
As for checking if the alarm exists, the PendingIntent with FLAG_NO_CREATE is exactly how you would do that.