【问题标题】:How can I set date on Alarmmanger?如何在 Alarmmanager 上设置日期?
【发布时间】:2013-10-20 12:45:42
【问题描述】:

我已尝试使用此代码在 3 月开始发出警报。 但它不起作用。 我该如何解决这个问题?

APP内容


当我们按下按钮(click1)时,如果是三月一号,Alarmmanger 会调用intent。 所以这个应用程序会打印新的 Activity。


错误


当我们按下按钮(click1)时,任何时候Alarmmanger都会调用intent。


我该如何解决??

-对不起我的英语不好..ㅠㅠ

public class MainActivity extends Activity {

private AlarmManager alarm;

private NotificationManager notification;
Calendar calendar = Calendar.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);}
    public void click1(View v) {
    notification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());


    calendar.clear();
    calendar.set(cal.YEAR,3,1);


    setAlarm();
    }



private void setAlarm(){

alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent());


}
private PendingIntent pendingIntent() {
    // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    PendingIntent p = PendingIntent.getActivity(this, 0, i, 0);
    return p;

}

【问题讨论】:

  • 这可能不是你的问题,但 March 的值是 2。为避免此类错误,请使用 Calendar 常量,在这种情况下为 Calendar.MARCH

标签: java android date calendar alarmmanager


【解决方案1】:

您不需要创建另一个日历来获取实际年份。
当您请求新的 Calendar 实例时,它将使用实际日期和时间进行初始化 试试这个:

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
calendar.set(year,Calendar.MARCH,1);

另外一件事,如果您将闹钟设置为过去的时间,闹钟总是会响起。

【讨论】:

  • 什么不行?编辑您的问题并尝试更好地解释问题
  • 哦!我错过了最后一句话!
  • 很高兴帮助您。如果您的问题得到解决,您必须接受这个作为答案。
【解决方案2】:

tl;博士

LocalDate.of( 2016 , Month.MARCH , 1 ).atStartOfDay( ZoneId.of( "America/Montreal" ) ).toInstant().toEpochMilli()

避免使用旧的日期时间类

您正在使用麻烦的旧旧日期时间类。现在被 java.time 类所取代。

LocalDate

LocalDate 类表示没有时间和时区的仅日期值。

LocalDate ld = LocalDate.of( 2016 , Month.MARCH , 1 );

您可以传递一个数字而不是 Month 枚举对象,1 月至 12 月的编号为 1-12。

时区

对于闹钟,我们需要一个时间。你可以得到一天的第一刻。这通常是时间00:00:00,但并非总是由于夏令时 (DST) 或其他异常情况。让 java.time 确定时间。

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,法国巴黎午夜过后几分钟是新的一天,而魁北克蒙特利尔仍是“昨天”。

ZonedDateTime

申请ZoneId 以获得ZonedDateTime

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;

如果您的警报库要求将这一时刻表示为自 1970 UTC 开始的纪元以来的毫秒数,请从提取的 Instant 对象中查询 long 整数。

long millisecondsSinceEpoch = zdt.toInstant().toEpochMilli() ;

关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date.Calendarjava.text.SimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到 java.time。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。

大部分 java.time 功能在ThreeTen-Backport 中向后移植到Java 6 和7,并进一步适应ThreeTenABP 中的Android(参见How to use…)。

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuarter 等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多