如何重复闹钟直到特定日期

时间:2017-02-12 10:01:45

标签: android broadcastreceiver alarmmanager android-pendingintent repeatingalarm

我正在尝试在两个日期之间设置重复闹钟。因为我首先使用开始日期设置重复警报,然后在最后一个日期设置一个警报,因为我在取消日期使用其他广播接收器,以便在最后一天到来时,它被取消。但即使在最后一天,我的警报也没有进入取消广播接收器,简而言之,它根本没有被取消。 这是我的主要代码,我正在设置两个警报:

  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

现在这是我用于生成通知的普通广播接收器

    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
       }

这是我的取消广播接收器

       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);
}

}

0 个答案:

没有答案