我已经创建了一个服务,它将由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);
请记住,SetExact()会降低操作系统最小化电池使用的能力。 –
我不能使用setExact因为分API必须19和我的应用程序分钟API是14 – muhwid
不幸的是setExact()仅适用于API 19+,所以你必须使用()设置为14至18,这将是不准确的。我会编辑我的答案。 – CoderP