title: Foreground service not working in android oreo
body: <p>In my app, it loads a url(API) after every 15 seconds in the background. I dont need any kind of UI in my app so I just load the url in my service class. To work properly on oreo+ devices I used startForgroundService(). But it is not working in Oreo after the app is killed. My testing device is OPPO Oreo 8.1.0. It is perfectly working on Samsung 7.0.</p>
  
  <p>Here is my Service code : </p>
  
  <pre><code>public class MyService extends Service {
      Handler handler = new Handler();
      Runnable runnable;
      int delay = 15 * 1000;
      String data;
  
      @Override
      public IBinder onBind(Intent intent) {
              return null;
      }
  
      @Override
      public int onStartCommand(final Intent intent, int flags, int startId) {
              data = intent.getStringExtra("data");
              loadURL(data);
  
              Intent notificationIntent = new Intent(this, MainActivity.class);
              PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  
              Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                      .setContentTitle("URL App status :")
                      .setContentText("The app is running in background")
                      .setSmallIcon(R.drawable.ic_launcher_foreground)
                      .setContentIntent(pendingIntent)
                      .build();
              startForeground(1, notification);
  
              handler.postDelayed(runnable = new Runnable() {
                      public void run() {
                              Toast.makeText(MyService.this, "" + data, Toast.LENGTH_SHORT).show();
                              loadURL(data);
  
                              handler.postDelayed(runnable, delay);
                      }
              }, delay);
              return START_STICKY;
      }
  
      @Override
      public void onDestroy() {
              stopSelf();
      }
  
      public void loadURL(String data) {
              try {
                      RequestFuture&lt;JSONObject&gt; requestFuture = RequestFuture.newFuture();
                      final String mURL = "http://localhost/att.php?emp_id=" + data + "&amp;status=&amp;submit=Insert";
                      JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                              mURL, new JSONObject(), requestFuture, requestFuture);
                      MySingleton.getInstance(this).addToRequestQueue(request);
                      Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
  
              } catch (Exception e) {
                      Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
              }
      }
  } 
  </code></pre>
  
  <p>App.class for notifications : </p>
  
  <pre><code>public class App extends Application {
  public static final String CHANNEL_ID = "channel ID";
  
  @Override
  public void onCreate() {
      super.onCreate();
      createnotificationChannel();
  }
  private void createnotificationChannel(){
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O){
          NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "URL channel ID",
                  NotificationManager.IMPORTANCE_DEFAULT);
          NotificationManager notificationManager = getSystemService(NotificationManager.class);
          notificationManager.createNotificationChannel(serviceChannel);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And the MainActivity where the foreground service is started :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  String data;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      data = getIntent().getStringExtra("ID");
      Intent intent = new Intent(MainActivity.this, MyService.class);
      intent.putExtra("data", data);
      ContextCompat.startForegroundService(this,intent);
  }
  </code></pre>
  
  <p>I have searched alot but I cant fint out what is wrong with my code. Just need help !</p>