1

I have service run in background.
I want to call this service at specific interval.
if user provide values 10, 20, 30 than service should call after 10 min,20 min and 30 min receptively.

How can I do above thing?

dev
  • 461
  • 3
  • 6
  • 18
  • Use alarmmanager to fire your service. Search for alarm manager and you will get numerous examples. – Tarun Jun 10 '13 at 07:04
  • but alarm manager calls twice. first when i set alarm and another time after provided delay. I want to call only one time. i.e. if i have call setalarm at 10:20:25 and provide time for 5 sec than it trigges two times at 10:20:25 and 10:20:30. – dev Jun 10 '13 at 07:10
  • Post your alarm code.. You can check out http://stackoverflow.com/a/17017471/786337 – Tarun Jun 10 '13 at 07:12

3 Answers3

1

AlarmManager will help you :)

It allows you to set up a schedule to launch your application's components during the specified time range

Update

To instantiate an AlarmManager that will go into play at the specific period after it's instantiating, configure it with setRepeating() method and PERIOD parameter added to SystemClock.elapsedRealime():

AlarmManager mgr = (AlarmManager)getSystemService(ALARM_SERVICE);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
                     SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi); // Here PERIOD is a value specified by you as PendingIntent object
Alex Bonel
  • 1,344
  • 1
  • 9
  • 22
  • but alarm manager calls twice. first when i set alarm and another time after provided delay. I want to call only one time. i.e. if i have call setalarm at 10:20:25 and provide time for 5 sec than it trigges two times at 10:20:25 and 10:20:30. – dev Jun 10 '13 at 07:09
  • It will not be called twice if you configure it with `setRepeating()` with specified `triggerAtMillis` parameter which could be an offset from the time you instantiate your `AlarmManager` – Alex Bonel Jun 10 '13 at 07:17
  • See my update answer. You should also consider recommendations from the official documentation about starting `Service` objects which says that >> If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available. – Alex Bonel Jun 10 '13 at 07:23
  • am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi); what should I pass insted of System.currentTimeMillis() – dev Jun 10 '13 at 07:23
  • Well, at least you should increase the second parameter (`triggerAtMillis`) `System.currentTimeMillis()` value by 5 seconds to trigger `AlarmManager` 5 seconds after it was instantiated – Alex Bonel Jun 10 '13 at 07:28
0

You can set an Alarm to have an Intent fired at specific interval. You can also set an inexact alarm if it is not critical that the alarm runs at the precise time.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • but alarm manager calls twice. first when i set alarm and another time after provided delay. I want to call only one time. i.e. if i have call setalarm at 10:20:25 and provide time for 5 sec than it trigges two times at 10:20:25 and 10:20:30 – dev Jun 10 '13 at 07:12
0
Calendar startTime = Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY, 10);
        startTime.set(Calendar.MINUTE, 20);
        startTime.set(Calendar.SECOND, 25);

        Intent dailyQuotes = new Intent(getActivity(), DailyQuotesService.class);
        AlarmManager am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getService(getActivity(), 0, dailyQuotes, 0);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(), 30000, pi);

here is my code and it call my service exactly on specified time. and then is repeated every 30 secs.

herrzura
  • 1
  • 4