How to set correct alarm/notification time in android?

问题: My problem is that I get my video game release dates data from a third party api, and that third party api's dates are in milliseconds, when I use java's Calendar class to...

问题:

My problem is that I get my video game release dates data from a third party api, and that third party api's dates are in milliseconds, when I use java's Calendar class to set my alarms, the alarm/notification gets called in the wrong time, what is the best way to set alarms in android so that they always get called at the correct time?

For example, the code I have now is weird. Game releases this Friday, I set an alarm for this game on Friday and this Thursday at 8pm Eastern, the alarm/notification gets fired. Why doesn't it fire at Friday midnight?

Here's my setAlarm method

    /**
     * Sets the next alarm to run. When the alarm fires,
     * the app broadcasts an Intent to this WakefulBroadcastReceiver.
     */
    public void setAlarm(Context context, Alarm alarm) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        // Passing our intent data
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        alarmIntent.putExtra("releaseId", alarm.getReleaseID());
        alarmIntent.putExtra("gameName", alarm.getGameName());
        alarmIntent.putExtra("gameId", alarm.getGameId());
        alarmIntent.putExtra("releaseDate", alarm.getDate());
        alarmIntent.putExtra("type", alarm.getType());
        // Note the release id is used here
        String alarmId = String.valueOf(alarm.getReleaseID()) + getTypeId(alarm.getType());
        PendingIntent pendingIntent  = PendingIntent.getBroadcast(context, Integer.parseInt(alarmId), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTimeInMillis(alarm.getDate());
        Date date = calendar.getTime();
        alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
    }

回答1:

after android 4.4 use setExact;after android 6.0 user setExactAndAllowWhileIdle link https://developer.android.google.cn/training/monitoring-device-state/doze-standby.html#restrictions https://developer.android.google.cn/about/versions/android-4.4.html#BehaviorAlarms

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
} else {
    alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}

回答2:

Try to use gregorian calendar https://developer.android.com/reference/java/util/GregorianCalendar.html

  • 发表于 2019-03-29 17:46
  • 阅读 ( 197 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除