0

I written a small application which invokes a service using AlarmManager with interval of the day. To test this app, i am using emulator 5.1 . When i set manually date and time(for current date or future date time) the AlarmManager invokes service for that day. When i am trying to change the date for checking whether it will work for other day by changing date then its not invoking service.

Here is my code.

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(calendar.HOUR_OF_DAY,13);
        calendar.set(calendar.MINUTE,20);
        calendar.set(Calendar.SECOND,10);

        alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,BackgroundService.class);
        pendingIntent = PendingIntent.getService(this, 0, intent, 0);


        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();

BackgroundService is service which popup with a Toast .

I have 2 issues.

  1. How can test AlarmManager , which we set with Interval of a day?
  2. The Alarm Manager is not exactly invoking at the second. For example , i set the alarm at 13:20:10 but its coming at 13:20:some Random second

Your help is much appreciated.

Thank you.

user2350138
  • 507
  • 2
  • 9
  • 27

1 Answers1

0
  1. RTC (Real Time Clock) is the clock that you can adjust, so you could possibly just change the current time programatically for a test case. See this answer
  2. You're using a alarmManager.setInexactRepeating method, which doesn't guarantee an exact time of alarm delivery. For more precise timings, use alarmManager.setRepeating, however keep in mind that it's not really recommended, as most use cases do not require exact timings and it will cause higher battery usage. See inexactRepeating and repeating
Community
  • 1
  • 1
maciekjanusz
  • 4,702
  • 25
  • 36
  • but if i am using setRepeating then how can i make an alarm everyday at particular time only once – user2350138 Sep 26 '15 at 17:43
  • By using the setReapting, although i changed date to next day irrespective of date and time its executing recursively.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60,pendingIntent); used adb shell to change date to next day. i actually set the time to 13:20. But when i set the date and time to next day 13:15 still the alarm is executing. – user2350138 Sep 26 '15 at 17:53