Android Alarm RTC无法创建警报

时间:2014-05-21 20:29:22

标签: android alarmmanager

在使用AlarmManager创建每日提醒时似乎遇到问题,我已经看到了一些帖子,但我仍然卡住了。我成功地使用了ELAPSED_REALTIME。

以下是我的BroadcastReceiver的相关部分:

public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD=5000;

@Override
public void onReceive(Context context, Intent i) {
    scheduleAlarms(context);
}

public static void scheduleAlarms(Context context) {
    AlarmManager alarmManager =
            (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent reminderServiceIntent = new Intent(context, ReminderService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context,0,reminderServiceIntent, 0);

    // Cancel all alarms before rescheduling them.
    try {
        alarmManager.cancel(pendingIntent);
    } catch (Exception e) {
        Log.e("rx", e.toString());
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE,0);


    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);

这是我的服务:

public class ReminderService extends IntentService {

    public ReminderService() {
        super("ReminderService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("rx", "I ran!");
    }
}

/ *更新* / 删除了关于3.1不允许精确重复计时器的评论,实际上是4.4

1 个答案:

答案 0 :(得分:1)

由于api 19,由于某种原因,警报不再准确(无论这意味着什么。)

但是,只要您的targetSdkVersion小于19,之前的行为仍然存在,这意味着您可以继续依赖警报。