title: How to Create Forground Service Never Stop when Application is Inactive
body: <p>I have created one application in which I want to start service First time open the application.when application is in foreground Every 5 or 10 minutes Local Notification received.but Notification received only When Application Active or Recent app.When Clear the Recent app Notification Does't receive.I wanted this service to keep running on the device.</p>
  
  <p>Sample code of created service and start services:
   Start the service:</p>
  
  <pre><code>    Intent i=new Intent(MainActivity.this,MyService.class);       
      startService(i);
  </code></pre>
  
  <p>The service:    </p>
  
  <pre><code>    public class MyService extends Service {
      final static String TAG = MyService.class.getName();
      ReceiverCall receiverCall;
  
  
      private static final int NOTIFICATION_ID = 1;
  
      MyService (){
          super();
          }
      class Mythread implements Runnable {
          int service_id;
  
          Mythread(int service_id) {
              this.service_id = service_id;
          }
  
          @Override
          public void run() {
              int i = 0;
              synchronized (this) {
                 // while (i &lt; 10) {
                      try {
                          wait(2500);
                          i++;
                          processStartNotification(String.valueOf(i));
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                 // }
              }
          }
      }
  
      @Override
      public void onCreate() {
           receiverCall= new ReceiverCall();
              super.onCreate();
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          super.onDestroy();
          Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();
  
  
          Intent myIntent = new Intent(getApplicationContext(), MyService.class);
          startService(myIntent);
  
  
  
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
          super.onTaskRemoved(rootIntent);
          Intent myIntent = new Intent(getApplicationContext(), MyService.class);
          startService(myIntent);
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
        Thread thread = new Thread(new Mythread(startId));
          thread.start();
  
          return START_STICKY;
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
      private void processStartNotification(String s) {
          // Do something. For example, fetch fresh data from backend to create a rich notification?
  
          final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
          builder.setContentTitle("Scheduled Notification")
                  .setAutoCancel(true)
                  .setColor(getResources().getColor(R.color.colorAccent))
                  .setContentText(" Notification Service"+s)
                  .setSmallIcon(R.drawable.ic_launcher_background);
          builder.setDeleteIntent(receiverCall.getDeleteIntent(this));
          final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
          manager.notify(NOTIFICATION_ID, builder.build());
  
      }
      }
  
  BroadcastReceiver Class :
  
      public class ReceiverCall extends BroadcastReceiver {
      private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
      private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
      private static final int NOTIFICATIONS_INTERVAL_IN_HOURS = 2;
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i("Service Stops", "Ohhhhhhh");
          context.startService(new Intent(context, MyService.class));
      }
      public static PendingIntent getDeleteIntent(Context context) {
          Intent intent = new Intent(context, ReceiverCall.class);
          intent.setAction(ACTION_DELETE_NOTIFICATION);
          return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      }
      }   
  </code></pre>
  
  <p>Manifest declaration:</p>
  
  <pre><code>      &lt;service
              android:name=".MyService"
              android:exported="true"
              android:stopWithTask="false"&gt;&lt;/service&gt;
  
           &lt;receiver android:name=".ReceiverCall"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.servicesex" /&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  </code></pre>
  
  <p>Is it possible to run this service always as when the application pauses and anything else. After some time my application goes pause and the services also go pause or stop. So how can I run this service in background and always.</p>