在一天中的特定时间设置警报

时间:2012-01-19 16:24:38

标签: android

我试图在一天中的某个时间设置闹钟,例如:20:15 这是我正在使用的代码,但它不会在20:15发布

        Intent intent = new Intent(AlarmActivity.this, MyBroadcastReceiver.class);
        intent.putExtra("Hekma", "One better than none");
        PendingIntent pintent = PendingIntent.getService(AlarmActivity.this, 0,intent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();



        cal.set(Calendar.HOUR_OF_DAY, 20);
        cal.set(Calendar.MINUTE, 15);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

2 个答案:

答案 0 :(得分:5)

我在我的项目中使用AlarmManager,它完美无缺。试试这可能会对你有所帮助:

Intent myIntent =  new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager =  (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

答案 1 :(得分:0)

尝试

alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() - System.currentTimeMillis(), pintent);

这应该得到两次之间的差异,因此需要多长时间才能让它消失。因此,它应该在你希望它消失的时候消失。