2

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);

    }

}

Benjamin Owen
  • 608
  • 2
  • 10
  • 28

1 Answers1

0

I have quite a lot of experience developing background services and alarm-scheduled events on Samsung flagship smartphones.

You can try disabling battery optimizations for all your components but even then, the OS will not reliably call your code.

In my book, it is a bug, but I'm sure Samsung and/or Google consider it a feature (resource management is their absolutely top priority).

deLock
  • 762
  • 8
  • 16