0

here is my code when i'm inserting time and date string in local database then also alarm receiver is set on the time which i insert in local database but the problem is this when i am inserting past time and date string in database it will trigger on current time while the time already passed.it works for latest time and date string

Calendar calender = Calendar.getInstance();
                    calender.clear();
                    calender.set(Calendar.MONTH, pickerDate.getMonth());
                    calender.set(Calendar.DAY_OF_MONTH, pickerDate.getDayOfMonth());
                    calender.set(Calendar.YEAR, pickerDate.getYear());
                    calender.set(Calendar.HOUR, pickerTime.getCurrentHour());
                    calender.set(Calendar.MINUTE, pickerTime.getCurrentMinute());
                    calender.set(Calendar.SECOND, 00);
                    SimpleDateFormat formatter = new SimpleDateFormat(getString(R.string.hour_minutes));
                    String timeString = formatter.format(new Date(calender.getTimeInMillis()));
                    SimpleDateFormat dateformatter = new SimpleDateFormat(getString(R.string.dateformate));
                    String dateString = dateformatter.format(new Date(calender.getTimeInMillis()));


                    AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
                    String alertTitle = mTitleText.getText().toString();
                    intent.putExtra(getString(R.string.alert_title), alertTitle);
              // broadcastCode++;
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), broadcastCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    broadcastCode+=1;
                    alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pendingIntent);
                    cv.put(mDbHelper.TIME, timeString);
                    cv.put(mDbHelper.DATE, dateString);

                db.insert(mDbHelper.TABLE_NAME, null, cv);
  • What should be the right behaviour ? Not set `alarmMgr` ? If so just add an if-condition to check if your date is in the past before setting an alarm – Gaëtan Maisse Apr 11 '17 at 05:30
  • Wait, I might've misunderstood. What do you want to happen if the time has already passed? – Mike M. Apr 11 '17 at 05:30
  • @MikeM. want alarm not fire when time pass, for this what i do here – ateeq shafique Apr 11 '17 at 05:51
  • @GaëtanMaisse, how i use if condition before setting alarm – ateeq shafique Apr 11 '17 at 05:55
  • There are examples of how to compare times in both links provided above, but essentially you just want to check if your `calendar` is after right now, and only set the alarm if it is. E.g., `if (calendar.after(Calendar.getInstance())) { set your alarm }`. Make sure to do that check _after_ all of your `calender.set()` calls. – Mike M. Apr 11 '17 at 05:56
  • @MikeM. let me try now – ateeq shafique Apr 11 '17 at 06:04
  • @MikeM. it work for me i just use before instead of after in condition, did u notice i am using broadcast increment in my code for creation of multiple alarms these alarm are fire properly when there time reaches but in case of cancel alarm it does not work properly means if i create 5 alarms, when i cancel alarm no 5 it canceled and previous 4 alarms work properly when there time reached if i delete alarms 1 which created at first it delete but when it time reaches it's fire and other 4 alarms are cancel what's wrong i'm doing there kindly guide what i do to right this. – ateeq shafique Apr 11 '17 at 07:56
  • I can't be certain what's wrong with your cancel code. I would recommend, though, that you save the `broadcastCode` for each alarm, too, so you know you're canceling the right one. – Mike M. Apr 11 '17 at 08:07
  • @MikeM. cancel code in other activity with the same broadcast code with the reference of the class from where alarm created, i use same pending intent with Create_note.broadcast code create_note class from where alarm trigger – ateeq shafique Apr 11 '17 at 08:28
  • I don't know. That doesn't really make sense to me. I'll see if I can reproduce the behavior, when I get chance later on. – Mike M. Apr 11 '17 at 08:52
  • @MikeM. thanks try it i stuck on this problem from couple of weak but did not find solution till now – ateeq shafique Apr 11 '17 at 09:46
  • OK, I just ran some tests, and it works as expected for me. It really sounds like you're still looping through all of your alarms when you're just trying to cancel one of them, and you've got an off-by-one error with with the `broadcastCode`, which would explain why the first one still fires when you're trying to cancel that one. – Mike M. Apr 11 '17 at 10:32
  • @MikeM. i think all other alarm get broadcast according to ist created because broadcast code intialize with 0 and increment when other alarm created, you have the same issue on cancel alarm when you are doing like me? – ateeq shafique Apr 11 '17 at 11:10
  • I didn't have any problems cancelling the alarms, whether it was the first one, a middle one, or the last one. I created multiple alarms, each time incrementing the `broadcastCode`, starting at zero. Each one I cancelled did not fire, and each one I did not cancel still fired right on time. I can't tell what your problem is, exactly, because you're not showing us how you're actually calling any of the code you've provided; neither here, nor in [your previous post](http://stackoverflow.com/q/43291826). – Mike M. Apr 11 '17 at 11:18
  • @MikeM. how i give post the whole code can you post the how you do this – ateeq shafique Apr 11 '17 at 11:45
  • Posting my test code wouldn't really help you, and would probably just confuse things. You can still [edit your other question where you asked about this](http://stackoverflow.com/posts/43291826/edit). You need to show exactly how you're setting the alarms - i.e., how you're calling the code block you've posted here - as well as how you're cancelling them - i.e., how you're calling your `delete()` method shown in the other question. – Mike M. Apr 11 '17 at 11:58
  • @MikeM. i post my delete code again please check it – ateeq shafique Apr 11 '17 at 12:44

0 Answers0