title: Worker-Thread ends prematurely
body: <p>I am targeting Oreo. As you know, oreo introduced limits on background task execution time. Workarounds are - according to google - to put the background task in the foreground. This is what I was trying to do, yet once the foreground service is running, it gets destroyed after some time.
  First the phone switches off it's screen, then once I activate it again, the background task continues. Sometimes onDestroy on the foreground service is called without the task being completed.</p>
  
  <p>My goal is to have all tasks being set by enqueueWork to be executed without ondestroy being called and without phone sleep mode to interrupt it.</p>
  
  <p><strong>ForeGroundService</strong></p>
  
  <pre><code>public class ForeGroundService extends JobIntentService {
  
      static final int JOB_ID = 1000;
      static final int ONGOING_NOTIFICATION_ID = 33;
  
      static void enqueueWork(Context context, Intent work) {
          enqueueWork(context, ForeGroundService.class, JOB_ID, work);
      }
  
      Notification.Builder notification;
      NotificationManager mNotificationManager;
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      void einleitung(String Titel, String Text)
      {
          Intent notificationIntent = new Intent(this, ForeGroundService.class);
          PendingIntent pendingIntent =
                  PendingIntent.getActivity(this, 0, notificationIntent, 0);
  
  
          mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              NotificationChannel channel = new NotificationChannel(Titel,
                      Text,
                      NotificationManager.IMPORTANCE_HIGH);
              channel.setSound(null,null);
              mNotificationManager.createNotificationChannel(channel);
          }
          notification =
                  new Notification.Builder(this,Titel)
                          .setContentTitle(Titel)
                          .setContentText(Text)
                          .setSmallIcon(R.drawable.kleinesicon)
                          .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                          .setContentIntent(pendingIntent)
                          .setTicker("setTicker");
          mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification.build());
          startForeground(ONGOING_NOTIFICATION_ID, notification.build());
      }
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      void vordergrund(String Titel, String Text)
      {
          notification.setContentTitle(Titel);
          notification.setContentText(Text);
          mNotificationManager.notify(ONGOING_NOTIFICATION_ID, notification.build());
      }
      PowerManager.WakeLock wakeLock;
      @RequiresApi(api = Build.VERSION_CODES.O)
      @Override
      protected void onHandleWork(Intent intent) {
          if (beginn) {
              einleitung("Test", "Test");
              beginn = false;
          }
          PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
          wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                  "MyWakelockTag");
          wakeLock.acquire();
          //Do Work
  
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Intent local = new Intent();
          local.setAction("de.test.action");
          this.sendBroadcast(local);
          stopForeground(true);
          //toast("Fertig");
          if (wakeLock != null)
              wakeLock.release();
      }
  
      final Handler mHandler = new Handler();
  }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      private int JI = 1000;
      private BroadcastReceiver updateUIReciver;
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      void somefunction(someparameters)
      {
          Intent mServiceIntent = new Intent();
          mServiceIntent.putExtra...
          ForeGroundService.enqueueWork(getBaseContext(),ForeGroundService.class,JI,mServiceIntent);
      }
  
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          unregisterReceiver(updateUIReciver);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          setTheme(R.style.AppTheme);
          super.onCreate(savedInstanceState);
          IntentFilter filter = new IntentFilter();
          filter.addAction("de.test.action");
          updateUIReciver = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, Intent intent) {
                  ForeGroundService.shouldContinue = false;
              }
          };
          registerReceiver(updateUIReciver,filter);
  
  
          btnB.setOnClickListener(new View.OnClickListener() {
              @RequiresApi(api = Build.VERSION_CODES.O)
              public void onClick(View v) {
                  if (startcondition)
                  {
  
                      Intent startIntent = new Intent(MainActivity.this, MyService.class);
                      startIntent.setAction(Constants.ACTION.START_ACTION);
                      startService(startIntent);
  
  
                      Intent serviceIntent = new Intent(MainActivity.this,ForeGroundService.class);
                      startForegroundService(serviceIntent);
  
                      somefunction(someparameters);
                  }
                  else
                  {
                      Intent stopIntent = new Intent(MainActivity.this, MyService.class);
                      stopIntent.setAction(Constants.ACTION.STOP_ACTION);
                      startService(stopIntent);
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>EDIT: I made it work with sandhya sasane's solution and</p>
  
  <pre><code>public int onStartCommand(Intent intent, int flags, int startId)
  {
      if (beginn) {
          executorService = Executors.newFixedThreadPool(1);
          beginn = false;
      }
      final Intent i2 = intent;
      executorService.execute(new Runnable(){
          @Override
          public void run(){
              abarbeiten(i2);
          }
      });
      return START_STICKY;
  }
  </code></pre>
  
  <p>Important is the 1 in newFixedThreadPool(1); to only have one thread run at once</p>