Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating from IntentService to JobIntentService for Android O

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

like image 243
Kunu Avatar asked Feb 15 '18 10:02

Kunu


People also ask

What can I use instead of IntentService?

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.

Why is JobIntentService deprecated?

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.

What is difference between service and IntentService in Android?

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.

What are the requirements for a JobIntentService?

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.


1 Answers

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.

like image 94
CommonsWare Avatar answered Sep 23 '22 09:09

CommonsWare