2

I'm trying to set repeating alarm between two dates. for that I set repeating alarm first using starting date and then just setting a single alarm on last date, for that i'm using other broadcast receiver on the cancel date, so that when the last day came, it got cancelled. but my alarm is not getting inside the cancelling broadcast receiver even on the last date, in short it is not getting cancelled at all. here is my main code where i'm setting both alarms:

  Log.v("setting range alarm","key range id = " + keyIds[j] + "time = " + RangeTimes[j]);

                          AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    // RangeTimes[] got the starting date
                          alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, RangeTimes[j], AlarmManager.INTERVAL_DAY,
                                  PendingIntent.getBroadcast(this, keyIds[j], alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));      
                          Log.v("setting range alarm","key range id = " + keyIds[j] + "Cancel time = " + CancelRangeTimes[j]);

                         // setting the last date in alarm  , cancelRangeTimes[] got the last date

                          Intent cancellationIntent = new Intent(this, CancelAlarmBroadcastReceiver.class);
                          cancellationIntent.putExtra("key", pendingIntent);
                          alarmManager.set(AlarmManager.RTC_WAKEUP, CancelRangeTimes[j],
                                  PendingIntent.getBroadcast(this, keyIds[j], cancellationIntent, PendingIntent.FLAG_UPDATE_CURRENT));  // for exact repeating THIS

now this is my normal broadcast reciver for generating notifications

    public void onReceive(Context context, Intent intent) {


    String[] myStrings = intent.getStringArrayExtra("strings");
    String pleaseText = intent.getStringExtra("text");
    int count = intent.getIntExtra("count", 0);
    createNotification(context, pleaseText, "trying other option", "Alert");

    }

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,0, new Intent(context,MainActivity.class),0);
    NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.cast_ic_notification_play)
            .setContentTitle(msg)
            .setTicker(msgAlert)
            .setContentText(msgText);

    mbuilder.setContentIntent(notificationIntent);
    mbuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);

    mbuilder.setAutoCancel(true); // noti dismisble when user swipe it away

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService((Context.NOTIFICATION_SERVICE));

   notificationManager.notify(1, mbuilder.build());  // changes from 1 to _id
       }

and this is my cancelling broadcast receiver

       public class CancelAlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {


    PendingIntent pendingIntent = intent.getParcelableExtra("key");
    Log.v("setting the cancel","inside broadCast reciver " + pendingIntent  );
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pendingIntent);
}

}

shahtaj khalid
  • 476
  • 7
  • 24

0 Answers0