警报管理器无法在android API 4.4中工作

时间:2017-11-20 12:53:22

标签: android alarmmanager

我想在Android应用程序中每天晚上10点触发警报管理器。它在Lolipop版本上面工作,但在KitKat版本中不起作用。

 public static void startAlarmManagerOutTime(Context context) {

    Calendar currentCal = Calendar.getInstance();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 22);
    calendar.set(Calendar.MINUTE,00);

    long intendedTime = calendar.getTimeInMillis();
    long currentTime = currentCal.getTimeInMillis();



    alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent gpsTrackerIntent = new Intent(context1, AlarmReceiver.class);

    PendingIntent  pendingOutTimeIntent = PendingIntent.getBroadcast(context, 2, gpsTrackerIntent, 0);

    if(intendedTime >= currentTime){
        // you can add buffer time too here to ignore some small differences in milliseconds
        // set from today
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingOutTimeIntent);

    } else{
        // set from next day
        // you might consider using calendar.add() for adding one day to the current day
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        intendedTime = calendar.getTimeInMillis();

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingOutTimeIntent);
    }

}

这是广播接收器

 public class AlarmReceiver extends WakefulBroadcastReceiver {

 @Override
  public void onReceive(Context context, Intent intent) {

   Toast.makeText(context,"AlarmReceiver",Toast.LENGTH_SHORT).show();

  }
 }

我已将这些值放在我的清单中:

 <receiver android:name=".com.test.Receiver.AlarmReceiver">
    </receiver>

1 个答案:

答案 0 :(得分:0)

据我所知,因为重复警报是不准确的。这意味着如果您将其设置为特定小时,则可以触发该日期附近的另一个时间。这是为了节省资源而添加的。 要检查是否设置了警报,我建议使用adb命令:

adb shell dumpsys alarm

如果您使用的是linux或mac,则可以在此之后添加一个grep来搜索您的应用包名称。以毫秒显示的时间,因此您必须将该数字转换为日期。

我忘记了一些事情,你使用的是WakefulBroadcastReceiver但是你已经宣布了权限 WAKE_LOCK