如何设置通知每年只发送一次?

How to set the notification just send once every year?

我想在每年5月8日到5月12日之间发送一次通知。

但我坚持比较不同的年份,如果时间在 5 月 8 日到 5 月 12 日之间,这导致每次用户启动应用程序或重新启动时都会发送通知。

这让我困惑了很多个小时,有人可以教我如何解决这个问题,我们将不胜感激。

提前致谢。

我的清单:

<application>
        <!--  -->
        <receiver android:name=".AlarmSetting.PlayReceiver">
            <intent-filter>
                <action android:name="play_hskay" />
            </intent-filter>
        </receiver>

        <!-- -->
        <receiver android:name=".AlarmSetting.AlarmInitReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
</application>

我的主要 activity 将保存 booleanyear:

//Setting
public void startAlarmSetting() {
    Toast.makeText(this, "Starting setting", Toast.LENGTH_SHORT).show();

    Calendar cal = Calendar.getInstance();

    int year = cal.get(Calendar.YEAR);

    Intent intent = new Intent(this, PlayReceiver.class);
    intent.putExtra("msg", "play_hskay");

    PendingIntent pi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

    long time = cal.getTimeInMillis();
    //Save the time when user start application
    SharedPreferences preferences = getSharedPreferences("time", MODE_PRIVATE);
    preferences.edit().putLong("saveTime", time)
            .putInt("saveFirstYear",2017)
            .putInt("saveNextYear",year)
            .putBoolean("saveBoolean",true)
            .apply();
}

我的PlayReceiver: 这行代码if ( neverSend || !(firstYear == nextYear)){让我很困惑

    public class PlayReceiver extends BroadcastReceiver {

        private Context context;

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

            Toast.makeText(context, "Starting playReceiver TDU", Toast.LENGTH_SHORT).show();

            Bundle bData = intent.getExtras();

            // load current time
            Calendar current = Calendar.getInstance();
            //set first time
            Calendar firstLimit = Calendar.getInstance();
            firstLimit.set(Calendar.MONTH, 4);
            firstLimit.set(Calendar.DAY_OF_MONTH, 8);
            firstLimit.set(Calendar.HOUR, 12);
            firstLimit.set(Calendar.MINUTE, 30);
            firstLimit.set(Calendar.SECOND, 30);
            //set second time
            Calendar secondLimit = Calendar.getInstance();
            secondLimit.set(Calendar.MONTH, 4);
            secondLimit.set(Calendar.DAY_OF_MONTH, 12);
            secondLimit.set(Calendar.HOUR, 12);
            secondLimit.set(Calendar.MINUTE, 30);
            secondLimit.set(Calendar.SECOND, 30);

            SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE);
            boolean neverSend = preferences.getBoolean("saveBoolean",false);
            //int firstYear = preferences.getInt("saveFirstYear",0);
            int firstYear = 2017;
            int nextYear = preferences.getInt("saveNextYear",0);
            //neverSend will be true when first time.
            if ( neverSend || !(firstYear == nextYear)){

            if (current.after(firstLimit) && current.before(secondLimit)) {
               if (bData.get("msg").equals("play_hskay")) {
                sendNotification();
     //change the nextYear and save it.
     Calendar calendar=Calendar.getInstance();
                    int sendYear = calendar.get(Calendar.YEAR);
                    preferences.edit().putInt("saveNextYear",sendYear).putBoolean("saveBoolean",false)
                            .apply();
                }
            } else {
                Toast.makeText(context, "out of the period", Toast.LENGTH_LONG).show();
            }
}
}

我的 AlarmInitReceiver:

public class AlarmInitReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Start~~~~TDU", Toast.LENGTH_LONG).show();

        SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE);
        long setTime = preferences.getLong("saveTime", 0);

        Intent intentForSetTime = new Intent(context, PlayReceiver.class);
        intentForSetTime.putExtra("msg", "play_hskay");

        PendingIntent pi = PendingIntent.getBroadcast(context, 1, intentForSetTime, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, setTime, pi);
    }
}

可能是因为您每次启动应用程序时都.putBoolean("saveBoolean",true)