As i read No matter what time period you define in alarm manager it fires after every 1 minutes above sdk 21. I am running a service in which i want my method to execute after every 5 minutes. This is the code i have done
public class Servicebackground extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("service started in onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = newIntent(this,MyLocationListener.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, pendingIntent);
System.out.println("service started in on create");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service stopped");
}
@Override
public void onCreate() {
return;
}
public class MyLocationListener extends BroadcastReceiver implements LocationListener {
@Override
public void onReceive(Context context, Intent intent) {
Intent ii = new Intent(context, Servicebackground.class);
startService(ii);
}
In the class i am calling the service i have done
startService(new Intent(this, Servicebackground.class));
But even if i have defined the time to be 5 minutes the method runs after every minute.I read that after sdk 21 no matter what time you define the method will execute after every 1 minute if you are using alarm manager.
Please tell me if there is any mistake. Or please suggest me any other way in which i would be able to run the service in background even if app is killed and the method executes after every 5 minutes.If there are other possible ways please suggest.