Previously I was using IntentService to send data to the server periodically. However, since Android O limiting background task and processes I am moving towards JobIntentService.
My Activity code to schedule an alarm
Intent intent = new Intent(BaseActivity.this, EventBroadcastReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, EventBroadcastReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every half hour
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
AlarmManager.INTERVAL_HALF_HOUR, pIntent);
And my Service is as follows
public class EventAnalyticsService extends JobIntentService {
@Override
protected void onHandleWork(@NonNull Intent intent) {
// Perform your task
}
@Override
public void onCreate() {
super.onCreate();
}
}
Receiver for this code is
public class EventBroadcastReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, EventAnalyticsService.class);
context.startService(myIntent);
}
}
However this is not working for Android O when app is in background and if I use context.startForegroundService(myIntent);
to start my service it is throwing exception as Context.startForegroundService() did not then call Service.startForeground()
Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher. IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.
This class has been deprecated in favor of the Android Jetpack WorkManagerlibrary, which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. Helper for processing work that has been enqueued for a job/service.
If the task doesn't require any and also not a very long task you can use service. If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread.
On any device pre-Android Oreo, the JobIntentService will set up Wake Locks through the PowerManager, so make sure you require the WAKE_LOCK permission in your manifest. When running on anything less than Android Oreo the service will start almost instantly. This ensures backwards compatibility with the same code.
JobIntentService
is there mostly for a service that will be invoked from the UI, such as a service to perform a large download, initiated by the user clicking a "Download!" button.
For periodic background work, use JobScheduler
and a JobService
. If you need to support older than Android 5.0, use a suitable wrapper library (e.g., Evernote's android-job
), one that will use JobScheduler
on newer devices and AlarmManager
on older devices.
Alternatively, stick with AlarmManager
, but use a foreground service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With