如何分别设置特定日期的通知,例如 1 天、3 天、7 天和 28 天

How to set notifications for particular days like, 1 day, 3days, 7 days and 28 days respectively

我正在做一个项目,如果用户离开应用程序,我需要在不同时间设置闹钟,例如 1 天、3 天、7 天、28 天后。 我可以使用

轻松设置每天的闹钟
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY, 16);
        calendar2.set(Calendar.MINUTE, 40);
        calendar2.set(Calendar.SECOND, 0);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

我终于做到了。下面是解决方案

AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);

    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent();

    if (screenPreferences.getInt("summary_page", 0) == 0 ) {
        if(screenPreferences.getInt("alarm_set", 0) == 0){
        screenPreferences.edit().putInt("alarm_set",1).commit();
        startNotifications(alarmManager, intentArray, 1); // 1 is to start notifications
        }
    }else {
        screenPreferences.edit().putInt("alarm_set",0).commit();
        startNotifications(alarmManager, intentArray, 0); // 0 is to stop notifications
    }

/************************************************************/
private void startNotifications(AlarmManager alarmManager, ArrayList<PendingIntent> intentArray, int onOrOff) {

    for (int i = 0; i < 5; ++i) {
        Intent intent = new Intent(MainActivity.this, StartReceiver.class);

        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, i, intent, 0);
        // Single alarms set for 1 day, 3 days, 7 days, 14 days, 28 days
        if(onOrOff == 1){
        long daySet = 60 * 60 * 24 * 1000;
        switch (i){
            case 0: daySet = 60 * 60 * 24 * 1000; // 1 day
                    break;
            case 1: daySet = 60 * 60 * 72 * 1000; // 3 days
                    break;
            case 2: daySet = 60 * 60 * 168 * 1000; // 7 days
                    break;
            case 3: daySet = 60 * 60 * 336 * 1000; // 14 days
                    break;
            case 4: daySet = 60 * 60 * 672 * 1000; // 28 days
                    break;
            default:
                    daySet = 60 * 60 * 24 * 1000; // default 1 day
                    break;
            }

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + daySet, pendingIntent1);
        intentArray.add(pendingIntent1);
        }
        else {
            alarmManager.cancel(pendingIntent1);
        }
    }
}