IOS equivalent of BackgroundFetch in Android

I would like to know if there is an analog of Android for iOS BackgroundFetch .

I would like my Cordova Android app to wake up every 15 minutes or so, checking for updates and performing various other tasks.

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

Since there is no Android version in this plugin, I will write it myself with pleasure; but first I would like to know how I will implement such a function in Android. Any suggestions?

+5
source share
2 answers

On Android, you can install AlarmManger to wake up every X milliseconds and run 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 default IntentService , which runs in the background, has some limitations.

You can also look at the WakefulIntentService external library ( https://github.com/commonsguy/cwac-wakeful ). I use this together with AlarmManager to run background tasks.

Update

Class OnAlarmReceiver

 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 } } 

Class YourService

 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 } } 
+1
source

You can use AlarmManager . In addition, you can also use AccountManager .

+1
source

Source: https://habr.com/ru/post/1235592/


All Articles