title: Background service is not working in Oreo
body: <p>I want to run my app in background if I kill the app instance also. But after I kill my app the service also stops working. Here is my code please any one help me to solve my issue.</p>
  
  <p>I followed this link for running in the background but it is not working if I remove the instance. Please can any one show me how to run a background service if the instance is removed also?</p>
  
  <p>This is my MainActivity</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ctx = this;
      setContentView(R.layout.activity_main);
      Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ALARM_REQUEST_CODE, alarmIntent, 0);
      mSensorService = new SensorService(getCtx());
      mServiceIntent = new Intent(getCtx(), mSensorService.getClass());
      if (!isMyServiceRunning(mSensorService.getClass())) {
          startService(mServiceIntent);
      }
  }
  </code></pre>
  
  <p>Ths is my service class</p>
  
  <pre><code>   public class SensorService extends Service{
  
  public int counter=0;
  public SensorService(Context applicationContext) {
      super();
      Log.i("HERE", "here I am!");
  }
  
  public SensorService() {
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId);
      startTimer();
      return START_STICKY;
  }
  @Override
  public void onDestroy() {
      super.onDestroy();
      Log.i("EXIT", "ondestroy!");
      Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
      sendBroadcast(broadcastIntent);
    }
  
  private Timer timer;
  private TimerTask timerTask;
  long oldTime=0;
  public void startTimer() {
      //set a new Timer
      timer = new Timer();
  
      //initialize the TimerTask's job
      initializeTimerTask();
  
      //schedule the timer, to wake up every 1 second
      timer.schedule(timerTask, 1000, 1000); //
  }
  
  /**
   * it sets the timer to print the counter every x seconds
   */
  public void initializeTimerTask() {
      timerTask = new TimerTask() {
          public void run() {
              Log.i("in timer", "in timer ++++  "+ (counter++));
          }
      };
  }
  
  /**
   * not needed
   */
  
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  </code></pre>
  
  <p>}</p>