0

I have created two back ground services in my application. I have to start my back ground service with in some intervals. So I am using alarm for this. One service have to start for every 15 minutes and another one for once per day. My code is here.

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 15);
        Intent intent = new Intent(this, TestService.class);

        pintent = PendingIntent.getService(this, 0, intent, 0);
        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);;

        i=15;                                 
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), i*1000, pintent);

         if (networkInfo != null && networkInfo.isConnected()) 
         {

                startService(new Intent(getBaseContext(), TestService.class));

         }
        else
        {


        }

I used like this. Its works finely in first time. Which means my alarm starts again after 15 mints in first time. Then it repeating for every 15 seconds. I don't know how to set the time properly for this. Can any body tell me to achieve this? Thanks in advance.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
malavika
  • 1,331
  • 4
  • 21
  • 54

1 Answers1

3
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hr);
cal.set(Calendar.MINUTE, minutes+15);//
cal.set(Calendar.SECOND, 0);

change this line...

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), i*1000, pintent);

to

long repeatingTime=15*60*1000;

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),repeatingTime, pintent);
Santhosh
  • 1,867
  • 2
  • 16
  • 23