Alarm Manager is triggered immediately in Android

三世轮回 提交于 2020-01-17 04:34:20

问题


Strangely, my Alarm Manger is calling the pending intent immediately, even though I have put a condition if the current time is greater than alarm time run one day later.

I have alarm Manger to trigger everyday at 10PM.

  Intent startServiceIntent = new Intent(this, numbers.class);
  this.startService(startServiceIntent);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(res.getString(R.string.hoursvalue))); //10
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.AM_PM, Calendar.PM);
            cal.set(Calendar.MILLISECOND, 0);

            long alarmtime = cal.getTimeInMillis();

            Log.e("ALarm","Time"+alarmtime);

            if (currenttime > alarmtime)
            {
                alarmtime = alarmtime + AlarmManager.INTERVAL_DAY;
                Log.e("Current Time","Greater"+alarmtime);
            }

            if (currentapiVersion >= 19)
            {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent2 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
                PendingIntent pi = PendingIntent.getService(this, 741258963, intent2, 0);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
            }
            else if (currentapiVersion >= 16 && currentapiVersion < 19)
            {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent3 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
                PendingIntent pi = PendingIntent.getService(this, 741258963, intent3, 0);
                am.setRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
            }

The Log shows the next day in milliseconds correctly but triggers immediately. What could be the issue here?

来源:https://stackoverflow.com/questions/25195187/alarm-manager-is-triggered-immediately-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!