I'm setting multiple alarms on repeat (everyday) on a button click. they are working fine if all of the time is greater then the time of button press.But if any time in the array has passed, it is generating the notification right after the button click and as many as passed. I have tried to put a condition to check if time has passed so set the alarm on the next day but it is not working I keep getting notification on the button click.
here is my mainActivity code for button click
int[] h = new int[C.getCount()];
int[] m = new int[C.getCount()];
long[] AllTime = new long[C.getCount()];
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("strings", DealTimes); // sending deal times thoruh string to AlertReceverrr !!!
for (int i = 0; i < C.getCount(); i++) {
h[i] = Integer.valueOf(DealTimes[i].split(":")[0]);
m[i] = Integer.valueOf(DealTimes[i].split(":")[1]);
// Toast.makeText(getBaseContext(), "Time at 16th index " + h + "min " + m, Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
calendar.set(Calendar.HOUR_OF_DAY, h[i]);
calendar.set(Calendar.MINUTE, m[i]);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
AllTime[i] = calendar.getTimeInMillis();
// checking if current time is greater the any of the time in array
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
if ( curHr > h[i])
{
calendar.add(Calendar.DATE, 1);
}
Toast.makeText(getBaseContext(), "Setting Alarm" + i, Toast.LENGTH_SHORT).show();
final int _id = (int) System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP, AllTime[i],
// PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,AllTime[i],AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// recinving noti within 30 seconds of button press, and generting as much noti as are passed. DUH. was reciving right on time witout delay before setting on repeat
and here Is my alertReciver code
public void onReceive(Context context, Intent intent) {
String[] myStrings = intent.getStringArrayExtra("strings");
Log.i("okk", "cathing intent values through break" + Arrays.toString(myStrings));
createNotification(context,"Time is here baby","5 sec hogae hen beta","Alert");
Log.i("okk", "cathing intent values through break" + Arrays.toString(myStrings));
}
public void createNotification(Context context,String msg,String msgText, String msgAlert)
{
final int _id = (int) System.currentTimeMillis(); // unique request code
PendingIntent notificationIntent = PendingIntent.getActivity(context,_id, new Intent(context,MainActivity.class),0); // changed from 0 to _id
NotificationCompat.Builder mbuilder= new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.cast_ic_notification_play)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
// now intent we want to fire when noti is clicked
mbuilder.setContentIntent(notificationIntent);
// how person is notified
mbuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mbuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService((Context.NOTIFICATION_SERVICE));
// Log.i("okk", "NOTIFIED " + intent.getExtras());
notificationManager.notify(1,mbuilder.build()); // changes from 1 to _id
}