I am making an application that uses a BroadcastReceiver and a WakefulServiceIntent to call a method in my application every 10 minutes. The problem is, sometimes it calls it, sometimes it doesn't. I need to call this method every 10 minutes, even if the app is running in the background or the screen is off. My current code is below. Should I not be using an AlarmManager to start my timer? I've heard that the AlarmManager isn't accurate, but so inaccurate to where it doesn't even go off?
AlarmManager starting code:
Intent intent = new Intent(getContext(), UpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + ((2 * 60) * 1000)), ((10 * 60) * 1000), pendingIntent);
BroadcastReceiver class:
public class UpdateReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, UpdateService.class);
startWakefulService(context, service);
}
}
WakefulIntentService class:
public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("updated automatically");
OverviewFragment.refresh(getApplicationContext());
UpdateReceiver.completeWakefulIntent(intent);
}
}