title: Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often in Android 8 & above
body: <p>I have recently replaced all my service to foreground services and JobIntentService since there are some background execution limits (<a href="https://developer.android.com/about/versions/oreo/background" rel="noreferrer">https://developer.android.com/about/versions/oreo/background</a>) in oreo and above. As per documentation, JobIntentService acts like Intent Service for Android 7 &amp; below and acts like JobScheduler for Android 8 &amp; above. I have noticed there is an issue in new JobIntentService provided by Google.</p>
  
  <p><strong>Android 8 &amp; above:</strong> </p>
  
  <p>There is a crash happening continuously in android 8 and above. There was a ticket raised here mentioning about the same issue <a href="https://issuetracker.google.com/issues/63622293" rel="noreferrer">https://issuetracker.google.com/issues/63622293</a> and I have added a temp fix suggested by few geeks. </p>
  
  <p><strong>Android 7 &amp; below:</strong>
  JobIntentService which acts like Intent Service is not getting stopped once the work is done.</p>
  
  <p>I have implemented JobIntentService within a service which triggers whenever some action is performed by a user.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>public class SampleJobIntentService extends FixedJobIntentService {
  
  public static void postData(Context context, String data) {
      Intent intent = new Intent(context, SampleJobIntentService.class);
              intent.setAction(INITIAL_ACTION);
              intent.putExtra(SAMPLE_ID, data);
              SampleJobIntentService.enqueueWork(context,intent);
  }
  
  public static void enqueueWork(Context context, Intent work) {
      SampleJobIntentService.enqueueWork(context, SampleJobIntentService.class, JOB_ID, work);
  
   @Override
      protected void onHandleWork(@NonNull Intent intent) {
          if (intent != null) {
              SampleRequest sampleRequest = requests.get(intent.getAction());
              if (sampleRequest != null) {
                  try {
                     // perform some networking operations
                  } catch (Exception ex) {
                      Log.d("Error for intent ");
                  }
                  Log.i("send action ");
              } else
                  Log.e("action not found for ");
          }
      }
      }
  </code></pre>
  
  <p>To avoid the crash with JobIntentService, I took few references from <a href="https://issuetracker.google.com/issues/63622293" rel="noreferrer">https://issuetracker.google.com/issues/63622293</a></p>
  
  <pre><code>public abstract class FixedJobIntentService extends JobIntentService {
  
      @Override
      GenericWorkItem dequeueWork() {
          try {
              return new FixedGenericWorkItem(super.dequeueWork());
          } catch (SecurityException ignored) {
              doStopCurrentWork();
          }
          return null;
      }
  
      private class FixedGenericWorkItem implements GenericWorkItem {
          final GenericWorkItem mGenericWorkItem;
  
          FixedGenericWorkItem(GenericWorkItem genericWorkItem) {
              mGenericWorkItem = genericWorkItem;
          }
  
          @Override
          public Intent getIntent() {
              if (mGenericWorkItem != null) {
                  return mGenericWorkItem.getIntent();
              }
              return null;
          }
  
          @Override
          public void complete() {
              try {
                  if (mGenericWorkItem != null) {
                      mGenericWorkItem.complete();
                  }
              } catch (IllegalArgumentException ignored) {
                  doStopCurrentWork();
              }
          }
      }
  }
  </code></pre>