2017-02-28 58 views
0

我已经创建了一个服务,它将由AlarmManager以自定义/手动时间触发。如果我设置AlarmManager将在02.15 PM运行,有时它会在02.10或在设置的时间之前运行。任何人都可以帮助我。Android:AlarmManager提前触发(最小API 14)

这是我的代码来设置时间:

Calendar now = Calendar.getInstance(); 
    int minute = TimerFormatHelper.getMinute();  
    now.set(Calendar.HOUR, now.get(Calendar.HOUR)); 
    now.set(Calendar.MINUTE, minute); 
    now.set(Calendar.SECOND, 0); 
    return now.getTimeInMillis(); 
    //it will return timestamp for time that set 

这个代码来设置报警:

AlarmManager alarmManager = (AlarmManager)  getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    long startTime = HereTimestampThatGenerateBefore; 
    Intent intentApp = new Intent(getApplicationContext(), TheServiceHere.class); 
    PendingIntent pendingIntentApp = PendingIntent 
      .getService(getApplicationContext(), 0, intentApp, PendingIntent.FLAG_ONE_SHOT); 
    alarmManager.set(AlarmManager.RTC, startTime, 
      pendingIntentApp); 

回答

0

使用alarmManager.setExact()方法代替alarManager.set()

按照文档:

此方法与set()类似,但不允许操作系统调整 交付时间。该警报将尽可能接近要求的触发时间 。注意:从API 19(KITKAT)报警 开始交付是不精确的:操作系统将会移动报警,以最大限度地减少唤醒和电池使用 。有新的API支持需要严格交付保证的应用程序 ;请参阅setWindow(int,long,long, PendingIntent)和setExact(int,long,PendingIntent)。应用 其targetSdkVersion比API 19早将继续看到 以前的行为中,所有报警准确交货时 要求。

if (android.os.Build.VERSION.SDK_INT >= 19) { 
    alarmManager.setExact(...); 
} else { 
    alarmManager.set(...); 
} 
+0

请记住,SetExact()会降低操作系统最小化电池使用的能力。 –

+0

我不能使用setExact因为分API必须19和我的应用程序分钟API是14 – muhwid

+0

不幸的是setExact()仅适用于API 19+,所以你必须使用()设置为14至18,这将是不准确的。我会编辑我的答案。 – CoderP

0

只需设置在now.set(Calendar.SECOND, 0); 1或2秒触发警报。 15::now.set(Calendar.SECOND, 1);

如果警告被设定为2:15,则这将一旦时间达到2触发1。

+0

我已经设置了0分,是否不受影响? – muhwid

+0

这是问题不要设置为0。 –

0

尝试使用,而不是

alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

这样的:

alarmManager.set(AlarmManager.RTC, startTime, 
     pendingIntentApp); 

这里targetCal来自TimePickerDialog

private TimePickerDialog.OnTimeSetListener timePickerListener = 
     new TimePickerDialog.OnTimeSetListener() { 
      public void onTimeSet(TimePicker view, int selectedHour, 
            int selectedMinute) { 

       Calendar calNow = Calendar.getInstance(); 
       Calendar calSet = (Calendar) calNow.clone(); 

       calSet.set(Calendar.HOUR_OF_DAY, selectedHour); 
       calSet.set(Calendar.MINUTE, selectedMinute); 
       calSet.set(Calendar.SECOND, 0); 
       calSet.set(Calendar.MILLISECOND, 0); 

       if(calSet.compareTo(calNow) <= 0) { 
        //Today Set time passed, count to tomorrow 
        calSet.add(Calendar.DATE, 1); 
       } 

       setAlarm(calSet); 

      } 
     }; 

,你可以设置在SetAlarm方法报警,你可以找到您的接收器

private void setAlarm(Calendar targetCal){ 

    mSetTime.setText(
      "\n\n***\n" 
        + "Alarm is [email protected] " + targetCal.getTime() + "\n" 
        + "***\n"); 

    Intent intent = new Intent(getBaseContext(), MyReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

}