title: Background Service with Notification
body: <p>I am developing an Android application which calls API's to fetch data. Now I want to perform this task in background and each time data is changed in the List view, a notification should be generated.</p>
  
  <p>How can I achieve this? </p>
  
  <p>How can I make my API called in background and how can i generate notification. 
  I am new to Services and BroadcastReceivers so help me</p>
  
  <p>I am calling the service this way:</p>
  
  <pre><code>startService(new Intent(this, MyService.class).putExtra("Background",true));
  </code></pre>
  
  <p>I created this code to testing purpose. To check if notifications can be called in background even if the app is closed.</p>
  
  <p>My Service Class</p>
  
  <pre><code>public class MyService extends Service {
  
  private Boolean isShowingNotification = true ;
  NotificationManager notificationManager;
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId);
  
  
      if (intent.hasExtra("Background")) {
          if (isShowingNotification) {
              StopImportantJob();
              stopSelf();
          } else
              DoImportantJob();
      } else {
          DisplayNotification("Now showing the demo");
      }
  
      return START_NOT_STICKY;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Toast.makeText(this, "On Create Called", Toast.LENGTH_SHORT).show();
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      notificationManager.cancelAll();
  }
  
  public void DisplayNotification(String message){
  
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
              new Intent(this, MainActivity.class), 0);
      Notification notification = new Notification.Builder(this)
              .setContentTitle(message)
              .setContentText("Touch to off Service")
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentIntent(pendingIntent)
              .setOngoing(false)
              .build();
  
      notificationManager.notify(0,notification);
  
  }
  
  public void DoImportantJob(){
  
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
              new Intent(this, MainActivity.class), 0);
      Notification notification = new Notification.Builder(this)
              .setContentTitle("New mail from " + "test@gmail.com")
              .setContentText("Subject")
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentIntent(pendingIntent)
              .setOngoing(false)
              .build();
  
      startForeground(1992, notification);
      isShowingNotification =true;
  }
  
  public void StopImportantJob(){
  
      stopForeground(true);
      isShowingNotification = false;
      if(false){
          if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N ){
              stopForeground(STOP_FOREGROUND_DETACH);
              stopForeground(STOP_FOREGROUND_REMOVE);
  
          }
  
      }
  }
  
  }
  </code></pre>
  
  <p>What I believe I should do is listed below, please correct me if I am wrong.</p>
  
  <ol>
  <li>Start Service in the onCreate() of the MainAcitivity of Application.  </li>
  <li>In the Service call I will create a method which will do the API call.  </li>
  <li>On notifyDataSetChanged(); will call the Notification method.  </li>
  <li>Now here is the question: In Service class the API method will be called in onCreate() or  onStartCommand().</li>
  </ol>