警报管理器未按时工作

Alarm Manager doesn't work on time

我有这个方法应该安排警报但是当时间到达时它不会启动 pendingintent ??

public void setAlarm(String name, long time) {
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent dialog = new Intent(this, SubActivity.class);
    dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(this, 0, dialog, 0);

    if (Build.VERSION.SDK_INT >= 19) {
        if (System.currentTimeMillis() < time) {
            am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
        }else{
            time+=(AlarmManager.INTERVAL_DAY*7);
            am.setExact(AlarmManager.RTC_WAKEUP, time, pi);
        }
    } else {
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, AlarmManager.INTERVAL_DAY * 7, pi);
    }
}

问题是您将 PendingIntent 用于 Activity,这不一定会使设备保持足够长的唤醒时间以使 Activity 开始。您必须将 PendingIntent 用于 BroadcastReceiver,它利用唤醒锁保持设备唤醒,直到您的应用程序代码可以 运行。 WakefulBrodcastReceiver is a good choice, or you can roll your own as needed. See this article for an explanation and sample of how to use alarms to wake the device: http://po.st/7UpipA