android AlarmManager 多次提醒

potential 发布于 2014/07/18 12:03
阅读 1K+
收藏 0
public void setTiming(ArrayList<Push> pushs) {
if (pendingIntents == null) {
pendingIntents = new ArrayList<Integer>();
}
if (am == null) {
am = (AlarmManager) getSystemService(ALARM_SERVICE);
}
// if (intent == null) {


for (int i : pendingIntents) {
PendingIntent StopPendIntent0 = PendingIntent.getBroadcast(
getApplicationContext(), i, intent,
PendingIntent.FLAG_UPDATE_CURRENT);


am.cancel(StopPendIntent0);
// pendingIntents.remove(StopPendIntent0)
}
pendingIntents.clear();
calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());


for (Push push : pushs) {
String time = push.getTime();
String content = push.getQuest_name();
String day = push.getDay();
calendar.set(stringTOint(day.substring(0, 4)),
stringTOint(day.substring(4, 6)) - 1,
stringTOint(day.substring(6, 8)),
Integer.parseInt(time.substring(0, 2)),
Integer.parseInt(time.substring(3, 5)));
// 将秒和毫秒设置为0
int id = new Random().nextInt(1000);
pendingIntents.add(id);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
intent.putExtra("content", content);
intent.putExtra("id", Integer.parseInt(push.getId()));
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
String tmps = "设置闹钟时间为:"
+ DateUtil.getDate2(calendar.getTimeInMillis());
System.out.println(tmps);
}

}

 push是一个自己建立的本地推送的类  ,从接口拿到push的推送集合 ,虚幻插入到Alarm 里面    设置的时间都是正确的 但是有时候会推送好几条 ,并且不是那个时间。求解答

加载中
0
obaniu
obaniu
不同厂商对AlarmManager出于各种原因动了手脚(比如小米),使得AlarmManager的实际提醒时间不是准确的
potential
potential
回复 @droidwolf : 貌似还是晚了 ,把手机日期调成19日 ,所有的日期都混乱的推过来了
obaniu
obaniu
@potential android 4.x之后android的AlarmManager已经采取了小米的类似做法,要想准时调用setExact,方法
potential
potential
但是nexus5 也会这样的
返回顶部
顶部