如何在提醒日期前一天设置闹钟,在同一日期提醒时间前两小时设置闹钟

时间:2017-11-28 12:20:07

标签: android android-intent alarmmanager alarm

我尝试在当前日期的两天后准确设置提醒。对于这个提醒,我想在一天之前设置闹钟以提醒,再过一次它应该在两天前的同一天响铃。

    public void forday() {
    SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
    int day = parseInt(dayFormat.format(myCalendar.getTime()));
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    int month = parseInt(monthFormat .format(myCalendar.getTime()));

    Calendar before1Day = Calendar.getInstance();
    Calendar oneDayBefore = Calendar.getInstance();
    Log.d("before1Day", "forday: "+before1Day);
    Log.d("oneDayBefore", "forday: "+oneDayBefore);

    oneDayBefore.set(Calendar.HOUR, hours);
    oneDayBefore.set(Calendar.MINUTE, minutes);
    oneDayBefore.set(Calendar.SECOND, 0);
    oneDayBefore.set(Calendar.MILLISECOND, 0);
    oneDayBefore.set(Calendar.DATE, day-1);
    oneDayBefore.set(Calendar.MONTH, (month-1));
    oneDayBefore.set(Calendar.AM_PM,before1Day.get(Calendar.AM_PM));
    Log.d("afteroneDayBefore", "forday: "+oneDayBefore);


    Calendar before2Hours = Calendar.getInstance();
    Calendar twoHoursBefore = Calendar.getInstance();
    Log.d("before2Hours", "forday: "+before2Hours);
    Log.d("twoHoursBefore", "forday: "+twoHoursBefore);
    twoHoursBefore.set(Calendar.HOUR, hours-2);
    twoHoursBefore.set(Calendar.MINUTE, minutes);
    twoHoursBefore.set(Calendar.SECOND, 0);
    twoHoursBefore.set(Calendar.MILLISECOND, 0);
    twoHoursBefore.set(Calendar.DATE, day);
    twoHoursBefore.set(Calendar.MONTH, (month-1));
    twoHoursBefore.set(Calendar.AM_PM,before2Hours.get(Calendar.AM_PM));
    Log.d("AftersettwoHoursBefore", "forday: "+twoHoursBefore);



    Intent intent = new Intent(this, MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

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


        alarmManager.set(AlarmManager.RTC_WAKEUP,oneDayBefore.getTimeInMillis(),pendingIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP,twoHoursBefore.getTimeInMillis(),pendingIntent);


    Toast.makeText(this, "Alarm set  "+oneDayBefore.getTime()+" in seconds",Toast.LENGTH_LONG).show();

    Toast.makeText(this, "Alarm set  "+twoHoursBefore.getTime()+" in seconds",Toast.LENGTH_LONG).show();
    Log.d("finaloneDayBefore", "forday: "+oneDayBefore.getTime());
    Log.d("finaltwoHoursBefore", "forday: "+twoHoursBefore.getTime());

}

在调用intent之前,我正在尝试的是完美的,但是在对单词调用意图后,我的警报日期将会改变,即一天即将到来,另一个在提醒日期之后。(例如我是试图在2017年11月30日14.30之前设置提醒在召唤意向之前设置一个警报设置2017年11月29日14.30并且还有一个是2017年11月30日12.30,但是在调用意图后警报改变日期就像第一个一样是Thu 11月30日02:30:00格林尼治标准时间+05:30 2017年第二名是格林威治标准时间12月1日00:30:00 GMT + 2017年5月30日。

我在哪里做错了?

0 个答案:

没有答案