当特定日历设置时间时,警报管理器不会关闭

时间:2014-10-12 07:37:08

标签: android

我试图在用户点击按钮后的特定时间(现在,周日上午12:36截至本帖)启动新意图。我查看了所有的日历课程​​教程,但似乎找不到什么错误。我已经测试了没有日历功能的代码,它运行正常。代码如下:

public void myClickHandler(View view){
    switch (view.getId()){
    case R.id.button_start:
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        AlarmManager alrmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alrmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

您未正确设置星期几。 Calendar.SUNDAY = 1,所以更改如下:

calendar.set(Calendar.DAY_OF_WEEK, 1);

或者,更好的是,使用类常量:

calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);