我通过BroadcastReceiver&amp ;;做了一个定时通知。 AlarmManager:
static public void setReminderNotification(Context c){
AlarmManager alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(c, ReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 12460, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
iTime = 24000;
if (iTime > 0) {
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + iTime, iTime, pendingIntent);
}else if(iTime == 0){
alarmManager.cancel(pendingIntent);
}
}
我认识到,警报不是每24秒启动一次。这取决于。我在接收器中进行了登录:(格式:H - m - s)
Post Nofification: 10 - 47 - 0
Post Nofification: 10 - 47 - 13
Post Nofification: 10 - 47 - 37
Post Nofification: 10 - 48 - 1
Post Nofification: 10 - 48 - 25
Post Nofification: 10 - 49 - 0
Post Nofification: 10 - 49 - 13
Post Nofification: 10 - 49 - 37
Post Nofification: 10 - 50 - 1
Post Nofification: 10 - 50 - 25
Post Nofification: 10 - 51 - 0
Post Nofification: 10 - 51 - 13
Post Nofification: 10 - 51 - 37
Post Nofification: 10 - 52 - 1
Post Nofification: 10 - 52 - 42
Post Nofification: 10 - 53 - 0
Post Nofification: 10 - 53 - 13
我想知道为什么通知有时会更短/更长。 任何想法如何解决这个问题?
答案 0 :(得分:2)
注意:从API 19(KITKAT)开始,警报传递不准确:操作系统将移动警报以最小化唤醒和电池使用。有新的API支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续查看之前在请求时准确传递所有警报的行为。
请参阅此处http://developer.android.com/reference/android/app/AlarmManager.html
答案 1 :(得分:1)
只要了解它,AlarmManager就不准确了 在新的API level 19 of AlarmManager中,你有
public void setExact (int type, long triggerAtMillis, PendingIntent operation)