【问题标题】:Android : AlarmManager triggers anytimeAndroid : AlarmManager 随时触发
【发布时间】:2014-05-18 14:40:42
【问题描述】:

我有以下代码应该在 2 天后触发警报。但是它每 2 小时触发一次,一些用户说他们也每 5 分钟触发一次。

settingDB.updateSetting("Notification",1);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
Calendar_Object.set(Calendar.MINUTE,01);
Calendar_Object.set(Calendar.SECOND, 0);
//Calendar_Object.set(Calendar.DAY_OF_WEEK, 1);

// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver

Intent myIntent = new Intent(Setting.this, TimeAlarm.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(Setting.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//Log.w("alarm set for " , Calendar_Object.getTime().toString ());

/*
 * The following sets the Alarm in the specific time by getting the long
 * value of the alarm date time which is in calendar object by calling
 * the getTimeInMillis(). Since Alarm supports only long value , we're
 * using this method.
 * 3*1000*24*3600
 */

//alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),3600*1000**24,pendingIntent);

alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), 86400000*2, pendingIntent);

你能帮我解决这个问题吗?

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    传递给警报管理器的日历对象设置为固定时间。这将导致警报在该时间到来时响起。

    【讨论】:

    • 这就是问题所在。我想补充一点,如果你想在日历中添加一定的天数,你可以使用这个:Calendar_Object.add (Calendar.DATE, numberOfDaysToAdd)
    • 感谢您的快速回复。我想要的是每两天触发一次警报。它的重复警报。但是,它会在每两个小时或多次触发后不断触发。我应该删除Calendar_Object.set(Calendar.HOUR_OF_DAY, 10); Calendar_Object.set(Calendar.MINUTE,01); Calendar_Object.set(Calendar.SECOND, 0);
    【解决方案2】:

    你也可以这样做

    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
                    + 60*1000,timeofAlarm pendingIntent);
    

    您传递的日历对象似乎造成了损害。按照我上面的例子,一旦警报会在几分钟后响起。只是为了检查警报是否正常工作,(您可能想随时添加,由您决定)。

    实时很有用(也许是推荐的)如果您希望闹钟在每个给定时间响起,而不管用户时间状态如何

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多