Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我选择了随机日期进行通知,但每天都会显示通知_Android_Notifications_Broadcastreceiver_Alarmmanager_Android Pendingintent - Fatal编程技术网

Android 我选择了随机日期进行通知,但每天都会显示通知

Android 我选择了随机日期进行通知,但每天都会显示通知,android,notifications,broadcastreceiver,alarmmanager,android-pendingintent,Android,Notifications,Broadcastreceiver,Alarmmanager,Android Pendingintent,我正在尝试在android level 26+中使用android studio设置通知,但如果我选择了随机天数进行通知,但通知每天都会显示 如何设置Calendar.DAYS\u OF_WEEK…..如今天是星期日,我正在设置Calendar.DAYS\u OF_WEEK,3(星期二)报警管理器应仅在每个星期二显示通知,但也在星期日显示通知 private void setNotification(AlarmList values) { Cursor data = mDatabase

我正在尝试在android level 26+中使用android studio设置通知,但如果我选择了随机天数进行通知,但通知每天都会显示

如何设置Calendar.DAYS\u OF_WEEK…..如今天是星期日,我正在设置Calendar.DAYS\u OF_WEEK,3(星期二)报警管理器应仅在每个星期二显示通知,但也在星期日显示通知

 private void setNotification(AlarmList values) {
    Cursor data = mDatabaseHelper.getItemID(values.getMedicineName());
    int itemID = -1;
    while (data.moveToNext()) {
        itemID = data.getInt(0);
    }
    if (itemID > -1) {

        Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
        intent.putExtra("name", values.getMedicineName());
        intent.putExtra("quantity", values.getQuantity());
        intent.putExtra("quality", values.getQuality());
        intent.putExtra("id", itemID);
        intent.putExtra("days", days);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), (itemID), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        for (int i = 0; i < 7; i++) {
            if (days[i]) {
                Calendar calendar = Calendar.getInstance();

                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                calendar.set(Calendar.DAY_OF_WEEK, (i + 1));

                long alarm_time = calendar.getTimeInMillis();
                
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time,
                        AlarmManager.INTERVAL_DAY*7, pendingIntent);
            }
        }
        finish();
        Intent activity = new Intent(AddMedicine.this, TabLayoutActivity.class);
        // i.putExtra("fragment id",1);
        startActivity(activity);
    }

这样设置日历是不可靠的。决不能使用每周的第几天调用
set()

如果您希望在下周三触发警报,您应该计算出下周三的日期(月、日、年),并在
日历上设置该日期

另见


如何为下周三的当前时间创建日历实例的示例:

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// dayOfWeek is 1..7 (SUNDAY to SATURDAY)
int daysToAdd = Calendar.WEDNESDAY - dayOfWeek;
// Subtract current day of week from the day of week you want
if (daysToAdd < 0) {
    // Current day of the week is after WEDNESDAY, so we go to next week
    daysToAdd += 7;
}
// If daysToAdd is zero, today is already WEDNESDAY!
if (daysToAdd != 0) {
    calendar.add(Calendar.DATE, daysToAdd);
}
Calendar Calendar=Calendar.getInstance();
int dayOfWeek=calendar.get(calendar.DAY/u周);
//星期一至星期六为1.7(星期日至星期六)
int daysToAdd=Calendar.星期三-dayOfWeek;
//从所需的星期几中减去当前星期几
如果(daysToAdd<0){
//一周的当前日期在星期三之后,所以我们转到下周
daysToAdd+=7;
}
//如果daysToAdd为零,那么今天已经是星期三了!
如果(daysToAdd!=0){
calendar.add(calendar.DATE,daysToAdd);
}

以这种方式设置日历是不可靠的。决不能使用每周的第几天调用
set()

如果您希望在下周三触发警报,您应该计算出下周三的日期(月、日、年),并在
日历上设置该日期

另见


如何为下周三的当前时间创建日历实例的示例:

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// dayOfWeek is 1..7 (SUNDAY to SATURDAY)
int daysToAdd = Calendar.WEDNESDAY - dayOfWeek;
// Subtract current day of week from the day of week you want
if (daysToAdd < 0) {
    // Current day of the week is after WEDNESDAY, so we go to next week
    daysToAdd += 7;
}
// If daysToAdd is zero, today is already WEDNESDAY!
if (daysToAdd != 0) {
    calendar.add(Calendar.DATE, daysToAdd);
}
Calendar Calendar=Calendar.getInstance();
int dayOfWeek=calendar.get(calendar.DAY/u周);
//星期一至星期六为1.7(星期日至星期六)
int daysToAdd=Calendar.星期三-dayOfWeek;
//从所需的星期几中减去当前星期几
如果(daysToAdd<0){
//一周的当前日期在星期三之后,所以我们转到下周
daysToAdd+=7;
}
//如果daysToAdd为零,那么今天已经是星期三了!
如果(daysToAdd!=0){
calendar.add(calendar.DATE,daysToAdd);
}

因此,如果我想每周三重复警报,我必须获取多少个日期……如果警报触发一年,它将产生52个日期的混乱……先生,还有其他建议吗?没问题。确定下一个星期三的日期,然后您可以将重复报警设置为每7天重复一次。您能告诉我如何从每周的第_天获取日期…就像我想从下周三获取日期一样我将如何获取日期…我在答案中添加了一个代码片段。我实际上并没有编译代码,所以里面可能有打字错误或其他错误。希望你能明白。所以如果我想每周三重复警报,我需要取多少个日期……如果警报触发一年,会引起52个日期的骚动……先生,还有其他建议吗?没问题。确定下一个星期三的日期,然后您可以将重复报警设置为每7天重复一次。您能告诉我如何从每周的第_天获取日期…就像我想从下周三获取日期一样我将如何获取日期…我在答案中添加了一个代码片段。我实际上并没有编译代码,所以里面可能有打字错误或其他错误。希望你能明白。