pending intent / alarm not working second time - android

My code is like this, on button click i execute
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()+(1000*5));
Intent intent = new Intent(LogoFrontScreen.this,Doubletest.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), alarmIntent);
PendingIntent alarmIntent1 = PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis()+(2000), alarmIntent1);
then on receiver there is a log but that log is called only once, why this is happening ?

In stead of
alarmMgr.setExact
Try
alarmMgr.setInexactRepeating
And for scheduling multiple alarm you need to use unique id every time creating
PendingIntent alarmIntent = PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);
The second argument of getBroadcast(i.e. 2 in your case) need to be different for every alarm.
Hope it will solve your problem :)

Related

How can i set the alarm on the particular date in android?

Explanation:
I have calendar in which i set the events on the particular day of the given month.The event is stored into the database. on the event day is occurs it is trigger an alarm to notice the user.
suppose, my event is save on the 29/05/2016 then my alarm is triggered on the particular date.
Notice:i have multiple event created on the particular month.e.g. on the 29th may or 30th may also.
MyQuestion is how can i fire the multiple alarm on the particular multiple days.
Please, understand the flow what i exactly want?
If you want to play alarm for particular date and time , following code may help you
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);
cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);
Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent,
PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,date.getYear());
calendar.set(Calendar.MONTH,date.getMonth()+1);
calendar.set(Calendar.DAY_OF_MONTH,date.getDate());
calendar.set(Calendar.HOUR_OF_DAY,time.getHours());
calendar.set(Calendar.MINUTE,time.getMinutes());
calendar.set(Calendar.SECOND,time.getSeconds());
Intent myIntent = new Intent(context, AlarmReciever.class);
myIntent.putExtra("ReminderDetails",reminderDetails);
myIntent.putExtra("requestCode", requestCode);
pendingIntent = PendingIntent.getBroadcast(context, (int) (requestCode), myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, date.getTime(),
interval, pendingIntent);
The following code here is good live for setting a calender time at the specified time and date.
And the most important thing here stands is that what do you pass in 2nd argument in alarmManager.setInexactRepeating(_) method,which is
date.getTime(), which is the only thing will work instead of using
calendar.getTimeinMillis() method, because that will return some unexpected time to be launched for intents.

Android set Exact Repeating Alarms WorkAround

I am trying to set repeating alarms in an exact fashion by manually setting an alarm to the next day the moment I receive it in a broadcast. However, I might be missing something and somehow its not working.
The Logic:
When I select the time from a time chooser, I set an exact alarm to the time given and start a pending intent which calls MyBroadCastReceiver.java (extends BroadcastReceiver) when its time. I then forward the intent with another pending intent to AlarmFireActivity. (AlarmFireActivity also has snooze which simply sets a pendingIntent to itself to fire after 5 mins.)
The MyBroadCastReceiver component hence only receives the actual alarms (not snoozes). And its functions are :
a) Get the non-repeating pending intent cancel it, then create another pending intent from the intent and then set it with the milliseconds set to the next day at the same time.
b) Start the AlarmFireActivity and show the alarm.
In AddAlarmActivity
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra(DBHelper.COLUMN_ID,alarm.getId());
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, reminder.getHourOfDay());
calendar.set(Calendar.MINUTE, reminder.getMinuteOfHour());
calendar.set(Calendar.SECOND,0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
BLog("Pending intent added at " + new SimpleDateFormat(utilFunctions.timeFormat).format(calendar.getTime()));
}
Snippet MyBroadCastReceiver.java TaskOne
Logic
Get the intent which MyBroadCastReceiver receives. Reuse the same intent in a new PendingIntent set to the next day at the same time.
Code
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
PendingIntent p1 = PendingIntent.getBroadcast(context, alarmCurrent.getReminderId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(p1!=null){
alarmManager.cancel(p1);
}
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, alarmCurrent.getReminderId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//for test
calendar.add(Calendar.MINUTE,1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent1);
}
MyBroadCastReceiver.java TaskTwo
Logic
If the time when the alarm was set was after the time the alarm is due, do not forward the pending intent to the AlarmFireActivity.
Else forward it to the AlarmFireActivity which shows the screen to dismiss/snooze the alarm.
Code
Calendar timeSet = alarmCurrent.getAlarmReminderSetTime();
Calendar alarmTime = Calendar.getInstance();
if(timeSet!=null && alarmTime.getTimeInMillis() > timeSet.getTimeInMillis()){
Intent intent1 = new Intent(context, AlarmFireActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.putExtra(DBHelper.COLUMN_ID,id);
String ext = extras.getString(DBHelper.TASK_TITLE);
if(ext!=null){
intent1.putExtra(DBHelper.TASK_TITLE,ext);
}
context.startActivity(intent1);
}else{
}
The BroadCastReceiver is doing the second task just fine. However, not the task one. Its not repeating/ resetting a new pending intent. I am guessing I must have messed up the intent/pendingIntent somewhere. I dont know which. Please help/
The solution is restart the phone. I have seen somewhere that there is a bug in a one of the os' 4.3 or so. A pending intent with a request id of 0 might fail at times. And once I restarted the phone it worked. However, there is a more standard solution which is :
Replacing
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
with
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, A_NUMBER_GREATER_THAN_ZERO + reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
so that you are avoiding a pending intent with 0 at all times. Thanks all!

cancel a particular alarm

I am setting alarms at diffrent time . I want to delete a particulat alarm.
Ex. I set 100 alarms at diiferents times , now I want to Delete an alarm set at 25 Feb 2012 10.45 AM. How Can I do that.
I written following code to set alarm.
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
GregorianCalendar gc=new GregorianCalendar();
gc.set(2012, 1, 22, 10, 42,0);
Intent intent = new Intent(this, AlarmService.class);
gc.set(Calendar.AM_PM,0);
final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), sender);
I have a broadcast receiver to recieve the alarm.
You need to save the request code of alarm,that is 1 in your case here. The 2nd parameter you pass in your PendingIntent.getBroadcast() method is your request code for a particular alarm.So when you save request code along with the time for what you set alarm,it would be easy for you to get the particular alarm instance and then you can cancel it using:
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, OneShotAlarm.class); //OneShotAlarm is the broadcast receiver you use for alarm
PendingIntent sender = PendingIntent.getBroadcast(context,alarm_request_code, intent, 0);
am.cancel(sender);
You should take care that each of your request code for different alarms should be unique,otherwise your alarm will be rescheduled with new time you set with the older request code.
Hope you get the point.
You might need to store it in some form of data structure to keep track of the time and the object. That being said, I have not tried storing AlarmManager objects before.

AlarmManager set to same BroadcastReceiver doesn't work

I want to be able to register two alarms to the same BroadcastReceiver. However, the first alarm never gets fired. How can I make this work?
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 5);
long trigger1 = now.getTimeInMillis();
now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 10);
long trigger2 = now.getTimeInMillis();
Intent startIntent = new Intent(AlarmStartReceiver.START_ALARM);
startIntent.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 4);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent startIntent2 = new Intent(AlarmStartReceiver.START_ALARM);
startIntent2.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 5);
PendingIntent startPIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, trigger1, startPIntent);
alarm.set(AlarmManager.RTC_WAKEUP, trigger2, startPIntent2);
Only the second one goes off. How can I make them both go off?
EDIT FOR ANSWER: Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method
android pending intent notification problem
Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method android pending intent notification problem
Are you looking for separate notification event for each alarm you are setting? Or it has to be same notification with number of alarm events showing on the status bar icon?
Look at this post on how you can use "setData()" to the intent to create separate alarms.
Alarm Manager - Scheduling multiple Non-repeating events

How to Call Alarm Manager at particular time?

here i used alarm manager to call service at particular time . I want to call that service at 23:59:00 . How can i call alarm manager at that particular time ? Please help me to solve this issue.
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.SECOND,what time to set);
Intent intent=new Intent(getApplicationContext(),StartServiceAlaramReceiver.class);
startAlaramServicePendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),startAlaramServicePendingIntent);
U can give time for alarmmanager using following way,
Intent activate = new Intent(this, AlaramActivity.class);
AlarmManager alarams ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarams = (AlarmManager) getSystemService(ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+5000, alarmIntent);
I find using the JodaTime library for anything with respect to date and time.
Intent intent=new Intent(getApplicationContext(),StartServiceAlaramReceiver.class);
startAlaramServicePendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
final DateTime todayE = (new DateTime()).minuteOfDay().withMaximumValue().minusMinutes(1);
AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,todayE.getMillis(),startAlaramServicePendingIntent);
The bellow link provide the detail description to set the Event that wil fire on the specified time. http://justcallmebrian.com/?p=129

Categories

Resources