【问题标题】:AlarmManager triggers PendingIntent too soon for the second timeAlarmManager 第二次过早触发 PendingIntent
【发布时间】:2012-11-23 13:06:31
【问题描述】:

我有这个代码:

设置闹钟:

public void setAlarm()
{
    Calendar Calendar_Object = Calendar.getInstance();

    Calendar_Object.add (Calendar.DAY_OF_YEAR, 1);

    Calendar_Object.set(Calendar.HOUR_OF_DAY, 0);
    Calendar_Object.set(Calendar.MINUTE, 0);
    Calendar_Object.set(Calendar.SECOND, 1);

    Intent myIntent = new Intent(Main.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(Main.this,
            0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),1000*60*60*24, pendingIntent);
}

广播接收器:

public class AlarmReceiver extends BroadcastReceiver {

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

      Intent myIntent = new Intent(context, NotificationService.class);
      context.startService(myIntent);
    }

} 

服务:

public class NotificationService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

}

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService( this.getApplicationContext().NOTIFICATION_SERVICE);

    Intent intent1 = new Intent(this.getApplicationContext(), ABC.class);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "xxx", System.currentTimeMillis());

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(this.getApplicationContext(),
            "abc", "xyz",
            pendingNotificationIntent);

    mManager.notify(0, notification);
    }
}

在 00:00:01 设置闹钟后,一切都很完美,但下次出现问题时。 PendingIntent 在 24 小时间隔之间触发 6 到 8 次。我不记得每个触发器的时间是否相同,但我希望每天一次。代码有什么问题?

【问题讨论】:

  • 为便于理解,请使用混合大小写的变量名,以小写开头且不带下划线。
  • 我不明白为什么Calendar_Object.add (Calendar.DAY_OF_YEAR, 1);
  • 你什么时候调用了setAlarm()方法,因为每次都会重置定时器
  • setAlarm() 在主活动中被调用。如果定时器被重置也没问题,因为每次重置都会设置相同的时间。 @nininho

标签: java android alarmmanager android-pendingintent


【解决方案1】:

这将创建一个每天 00:05 响起的闹钟,重要的部分是 AlarmManager.INTERVAL_DAY

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.HOUR, 24);

PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
mgr.cancel(pi);
mgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

另外,不要忘记在重新设置新警报时始终取消以前的类似警报,否则它们都会响起,旧设置警报和新警报,见上文mgr.cancel(pi);

【讨论】:

  • 同样的问题发生了。它在实际时间之前触发。有人可以帮忙吗? @marmor
  • 听起来你有很多以前设置的警报响了,尝试重新启动,然后调用上面的代码。重新启动应取消设置所有警报。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多