Java 如何为一周中的特定日期设置重复通知?

Java 如何为一周中的特定日期设置重复通知?,java,android,push-notification,repeat,dayofweek,Java,Android,Push Notification,Repeat,Dayofweek,我正在寻找设置一个星期天的通知。我在“警报”对话框中添加了一串天来显示这些天 我想在日历中设置用户在警报对话框中选择的日期。此外,这一点应该在下周的同一天重复 我已经把时间选择器对话框,以选择通知的时间。但是如果我使用c.getTime(),那么我会得到当前的日期和时间 如何设置其他日期的通知?如果今天是星期一,我想在星期三创建通知?如何在日历中设置wed c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, mi

我正在寻找设置一个星期天的通知。我在“警报”对话框中添加了一串天来显示这些天

我想在日历中设置用户在警报对话框中选择的日期。此外,这一点应该在下周的同一天重复

我已经把时间选择器对话框,以选择通知的时间。但是如果我使用c.getTime(),那么我会得到当前的日期和时间

如何设置其他日期的通知?如果今天是星期一,我想在星期三创建通知?如何在日历中设置wed

  c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);

        notification = c.getTime();
        notificationTime = df.format(notification);
        notifyTime.setText(notificationTime);
        Toast.makeText(getApplicationContext(),String.valueOf(notification),Toast.LENGTH_SHORT).show();
对于通知,我正在使用报警管理器

 private void setAlarm(Calendar targetmCalen) {

    Intent intent = new Intent(getBaseContext(),NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,targetmCalen.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY *7, pendingIntent);

    ComponentName receiver = new ComponentName(getApplicationContext(),NotificationReceiver.class);
    PackageManager pm = getApplicationContext().getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}
用于选择日期的警报对话框:

  selectDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);

            builder.setSingleChoiceItems(R.array.day_array, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {

                            String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);

                            selectDay.setText(day[item]);

                            dialog.dismiss();
                        }
                    });
            builder.show();
        }
    });
我是否必须在警报对话框中的日历实例中设置dayofweek?像

c、 设置(日历。每周的第几天)

编辑:

我试着这样做。但如果我多次选择“一天”的话,每次都可能提前一天进行

 selectDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);

            builder.setSingleChoiceItems(R.array.day_array, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {

                            String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);

                            selectDay.setText(day[item]);

                            dayOfWeek = day[item];

                            switch (dayOfWeek)
                            {
                                case  "Mon":
                                    c.set(Calendar.DAY_OF_WEEK,2);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;
                                case "Tue":

                                    c.set(Calendar.DAY_OF_WEEK, 3);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;
                                case "Wed":
                                    c.set(Calendar.DAY_OF_WEEK, 4);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Thu":
                                    c.set(Calendar.DAY_OF_WEEK, 5);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Fri":
                                    c.set(Calendar.DAY_OF_WEEK, 6);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Sat":
                                    c.set(Calendar.DAY_OF_WEEK, 7);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Sun":

                                    c.set(Calendar.DAY_OF_WEEK,1);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                            }

                            dialog.dismiss();
                        }
                    });
            builder.show();
        }
    });
请帮忙

public class TimerReceiver extends BroadcastReceiver {

public  void scheduleAlarms(Context paramContext) {

    Calendar calendar = Calendar.getInstance();

    if (strmyTime.contains("One Week")) {
        calendar.set(Calendar.DATE, 7);

        registerReceiver(paramContext, calendar, AlarmManager.INTERVAL_DAY * 7);
    }

}

public  void registerReceiver(Context paramContext, Calendar calender, long interval) {

    AlarmManager localAlarmManager = (AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE);
    PendingIntent localPendingIntent = PendingIntent.getService(paramContext, 0,
            new Intent(paramContext, NotificationService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    localAlarmManager.setRepeating(AlarmManager.RTC, calender.getTimeInMillis(), interval, localPendingIntent);

}

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


    scheduleAlarms(context);
    context.startService(new Intent(context, Notification.class));
        }
}


}我希望你已经解决了你的问题。但我还是会发布我的解决方案。因此,如果有人在这方面遇到困难

private void sendNotification(){
    Intent notificationIntent = new Intent(HomeActivity.this, NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            NOTIFICATION_REQUEST_CODE,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, CONST.NOTIFICATION_TIME_HOUR);
    calendar.set(Calendar.MINUTE, 0);
    for (int i = 1; i < 8; ++i){
        if (i != 6){
            calendar.set(Calendar.DAY_OF_WEEK, i);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CONST.NOTIFICATION_INTERVAL, pendingIntent);
        }
    }
}
private void sendNotification(){
意图notificationIntent=新意图(HomeActivity.this、NotificationReceiver.class);
PendingEvent PendingEvent=PendingEvent.getBroadcast(getApplicationContext(),
通知请求代码,
通知意图,
PendingEvent.FLAG_UPDATE_CURRENT);
AlarmManager AlarmManager=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM\u服务);
日历=Calendar.getInstance();
calendar.set(calendar.HOUR(日历时数),CONST.NOTIFICATION(持续通知时数);
calendar.set(calendar.MINUTE,0);
对于(int i=1;i<8;++i){
如果(i!=6){
calendar.set(calendar.DAY\u OF_WEEK,i);
alarmManager.setRepeating(alarmManager.RTC_唤醒,calendar.getTimeInMillis(),CONST.NOTIFICATION_间隔,PendingEvent);
}
}
}

此处
long NOTIFICATION_INTERVAL=7*24*60*60*1000&
int通知时间小时=9

我希望你已经解决了你的问题。但我还是会发布我的解决方案。因此,如果有人在这方面遇到困难

private void sendNotification(){
    Intent notificationIntent = new Intent(HomeActivity.this, NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            NOTIFICATION_REQUEST_CODE,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, CONST.NOTIFICATION_TIME_HOUR);
    calendar.set(Calendar.MINUTE, 0);
    for (int i = 1; i < 8; ++i){
        if (i != 6){
            calendar.set(Calendar.DAY_OF_WEEK, i);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CONST.NOTIFICATION_INTERVAL, pendingIntent);
        }
    }
}
private void sendNotification(){
意图notificationIntent=新意图(HomeActivity.this、NotificationReceiver.class);
PendingEvent PendingEvent=PendingEvent.getBroadcast(getApplicationContext(),
通知请求代码,
通知意图,
PendingEvent.FLAG_UPDATE_CURRENT);
AlarmManager AlarmManager=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM\u服务);
日历=Calendar.getInstance();
calendar.set(calendar.HOUR(日历时数),CONST.NOTIFICATION(持续通知时数);
calendar.set(calendar.MINUTE,0);
对于(int i=1;i<8;++i){
如果(i!=6){
calendar.set(calendar.DAY\u OF_WEEK,i);
alarmManager.setRepeating(alarmManager.RTC_唤醒,calendar.getTimeInMillis(),CONST.NOTIFICATION_间隔,PendingEvent);
}
}
}


此处
long NOTIFICATION_INTERVAL=7*24*60*60*1000&
int通知时间小时=9

将通知放入我已完成的
服务中。我收到通知,但不是在预定的日期。我如何设置日期?您可以创建一个日期d对象,然后创建c.setTime(d)请您用代码告诉我好吗?请检查编辑。我不确定您是否有当前日期,但如果有,请在“现在时间”中创建一个日期对象,并在其天数中添加7(因为您希望它在同一天),然后您可以c.setTime(日期对象)将通知放入我已完成的
服务中。我收到通知,但不是在预定的日期。我如何设置日期?您可以创建一个日期d对象,然后创建c.setTime(d)请您用代码告诉我好吗?请检查编辑。我不确定您是否有当前日期,但如果有,请在“现在时间”中创建一个日期对象,并在其天数中添加7(因为您希望它在同一天),然后您可以c.setTime(日期对象)重复报警I can do Interval_day*7。但如何设置日期?对于重复报警,我可以设置间隔天*7。但我该如何设置日期呢?嗨@shafi,想知道“通知请求代码”是否每次都需要是唯一的吗?@Energy不,不是。你可以把它作为整个应用程序的常量。你能给我一个建议吗?目前,我允许用户选择他们想要的任何一天。(就像我给7(周一到周日)复选框一样)。假设用户选择Mon,Tue。我在forloop中运行上述sendNotification(),并将“day”作为参数。但是,如果我先选中“今天”复选框,它就会工作。例如,“今天”是星期二。然后我选择了星期二和星期一作为复选框。然后它就起作用了。但另一轮(周一和周二)没有。在我拿到你的密码之后。它起作用了。以前我的forloop在sendNotification()部分之外。这是出于意图吗?因为如果我在outsie上提出forloop,那么每次我都会创造一个新的意图。但是如果我移动到内部,它只使用一个相同时间的意图通知?我不知道为什么,但你不需要每次都创建意图。因为目的只是为了指导发生后该做什么。所以每次都是一样的。现在,对于建议部分,我认为缺少的是,这种方法将今天作为基本事实。因此,为了让它在任意一天工作,你必须从今天开始计算。i、 今天是星期三,所以星期天还有四天。照此计算。我希望有帮助。很好,Luckhi@shafi,想知道“通知请求代码”是否需要每次都是唯一的吗?@Energy不,不是。你可以把它作为整个应用程序的常量。你能给我一个建议吗?目前,我允许用户选择他们想要的任何一天。(就像我给7(周一到周日)复选框一样)。假设用户选择Mon,Tue。我在forloop中运行上述sendNotification(),并将“day”作为参数。但是,如果我先选中“今天”复选框,它就会工作。