广播后,Android会为其他日期重新安排一次性通知吗?

时间:2015-04-05 14:51:01

标签: android alarmmanager android-notifications repeat

我设法做了一次,重复通知工作正常,但每个月重复我认为我们需要再次设置通知作为一次性通知,因为有些月份是30天,其他31个等等。所以想法是当最初设置警报时,使用当前月份,并根据我们可以设置AlarmManager.INTERVAL_DAY + 31或+ 30等。但问题是它将每31天重复此警报。所以我认为如果我们将其设置为一次性闹钟并且一旦闹钟响起,我们就可以重新安排相同的闹钟,但只更改AlarmManager.INTERVAL_DAY + (currentMonthsNumberOFDays)

原始闹钟响起后,在后台执行此操作的最佳方法是什么?以下是BroadcastReceiver,它一次性通知一次,并重复通知,但每月重复一次。

package com.mypackage.myapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {

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

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
        System.out.println("Notification ID is: "+ id);
    }
}

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。我不确定这是否是最好的方式,但它确实有效。

我已将以下内容添加到BroadcastReceiver

 Long futureTimeDifference = intent.getLongExtra("futureTimeDifference", 0); // Receive the time difference in milliseconds from currenttime in milliseconds and the future set date milliseconds
    futureTimeDifference = futureTimeDifference + System.currentTimeMillis();// get the next schedule date time inmilliseconds
    String repeatType = intent.getStringExtra("getRepeatType");// Receive the repeat type

if(repeatType.equals("mondayToFriday")) {
        //handled in NotificationPublisherWeekDays BroadcastReceiver
    }
    else if(repeatType.equals("saturdayAndSunday")) {
        //handled in NotificationPublisherWeekEnd BroadcastReceiver
    }
    else if(repeatType.equals("oneMonth")) {

        Date todaysDate = new Date();// initialize a new date object
        Calendar getCurrentDate = Calendar.getInstance();// Initialize a new Calendar object
        getCurrentDate.setTime(todaysDate); //Set the calendar to todays date
        int currentMonth = getCurrentDate.get(Calendar.MONTH); // Assign the current month in integer

        if (currentMonth == Calendar.JANUARY || currentMonth == Calendar.MARCH || currentMonth == Calendar.MAY || currentMonth == Calendar.JULY || currentMonth == Calendar.AUGUST || currentMonth == Calendar.OCTOBER || currentMonth == Calendar.DECEMBER){
            futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 31);
        }
        if (currentMonth == Calendar.APRIL || currentMonth == Calendar.JUNE || currentMonth == Calendar.SEPTEMBER || currentMonth == Calendar.NOVEMBER){
            futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 30);
        }

        if  (currentMonth == Calendar.FEBRUARY){//for february month)
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
            if(cal.isLeapYear(2015)){//for leap year february month
                futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 29);
            }
            else{ //for non leap year february month
                futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 28);
            }
        }

    }

    // If it is not a one time notification (repeat is not off) reschedule the notification
    if(!repeatType.equals("Off")) {

        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent rescheduleNotificationIntent = new Intent(context, NotificationPublisher.class);
        rescheduleNotificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
        rescheduleNotificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
        PendingIntent rescheduleNotificationPendingIntent = PendingIntent.getBroadcast(context, 0, rescheduleNotificationIntent, 0);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, futureTimeDifference, rescheduleNotificationPendingIntent);
        System.out.println("Alarm set again - futureTime is: " + futureTimeDifference);
    }