5

I would like to know if there is an Android counterpart for iOS BackgroundFetch feature.

I would like my Android app using Cordova to wake up every 15 minutes or so and check for updates and perform some other miscellaneous tasks.

In iOS, I was able to do this by using cordova-background-fetch plugin.

Since there is no Android version of that plugin, I am happy to write it myself; but I would first like to know how I would go about implementing such feature in Android. Any suggestions?

mc9
  • 6,121
  • 13
  • 49
  • 87

2 Answers2

1

You can use AlarmManager. Besides that you can also use AccountManager.

Eric B.
  • 4,622
  • 2
  • 18
  • 33
1

In Android you can set AlarmManger to wakes up every X milliseconds and run a PendingIntent.

This code looks something like this.

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime()+60000,
                    PERIOD,
                    pi);

Android's default IntentService that runs in the background has some limitations.

You can also take a look at external libary WakefulIntentService (https://github.com/commonsguy/cwac-wakeful). I use that along with AlarmManager to run background tasks.

Updated:

OnAlarmReceiver class

public class OnAlarmReceiver extends BroadcastReceiver {

    public static String TAG = "OnAlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Waking up alarm");
        WakefulIntentService.sendWakefulWork(context, YourService.class); // do work in the service class
    }

}

YourService class

public class YourService extends WakefulIntentService {

      public static String TAG = "YourService";

      public YourService() {
          super("YourService");
      }

      @Override
      protected void doWakefulWork(Intent intent) {
          Log.d(TAG, "Waking up service");

        // do your background task here
      }
}
Sharjeel
  • 15,588
  • 14
  • 58
  • 89
  • This seems to be a good starting point. I implemented `OnAlarmReceiver` class by extending `BroadcastReceiver`. Do I need to call `registerReceiver` somewhere to listen to the intent that is broadcast in the third line? Couldn't find `registerReceiver` in `WakefulIntentService`'s codebase, and I am confused. – mc9 Nov 10 '15 at 05:52
  • @MikeC I updated answer with complete example. You will also have to define your service and receiver in Manifest file to make it work. – Sharjeel Nov 10 '15 at 05:59
  • I did not end up using `WakefulIntentService`, but thanks for pointing me to the right direction. – mc9 Nov 11 '15 at 04:49
  • btw, how do you clear the intents you set by `AlarmManager #setRepeating` when the app terminates? I am happy to post a separate question if answer is complicated. – mc9 Nov 11 '15 at 04:50