title: Running a Service forever in background - Android
body: <p>I have created a <code>Service</code> in my android application which starts automatically on <code>BOOT_COMPLETE</code> through <code>BroadcastReceiver</code>. And That is working perfectly fine. But this service performs the task that I have define inside <code>onCreate()</code> method, only once. On the Other hand I want to run the <code>Service</code> forever in background. Actually inside <code>onCreate()</code> method I am reading data from my database and I am generating notifications if required. And the notifications can be generated any time, so therefore I want to run my service forever. I am new to Android, and I have seen may examples &amp; tutorial but they did not helped. So kindly Answer that how can I run my <code>Service</code> forever.</p>
  
  <p><strong>This is code of Service:</strong></p>
  
  <pre><code>package com.example.abc.project1;
  
  import android.app.*;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.*;
  import org.json.*;
  import java.io.*;
  import java.net.*;
  import java.util.*;
  
  
  public class HelloService extends Service {
  
      private static final String TAG = "HelloService";
      public static boolean isRunning  = false;
  
      @Override
      public IBinder onBind(Intent arg0) {
          return null;
      }
  
  
      @Override
      public void onCreate() {
          isRunning = true;
          new Thread(new Runnable() {
              @Override
              public void run() {
                  /*Here I want to do my task forever (reading from database &amp; generate notifications)*/
              }
          }).start();
      }
  
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          return Service.START_STICKY;
      }
  
      @Override
      public void onDestroy() {
          isRunning = false;
      }
  
  }
  </code></pre>
  
  <p><strong>This is code of BroadcastReceiver</strong></p>
  
  <pre><code>package com.example.abc.project1;
  
  import android.content.*;
  import android.util.Log;
  
  public class MyBroadcastreceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent startServiceIntent = new Intent(context, HelloService.class);
          context.startService(startServiceIntent);
      }
  }
  </code></pre>
  
  <p><strong>This is part of AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.abc.project1"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="com.google.android.providers.gef.permission.READ_GSERVICES" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"&gt;&lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_CATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;meta-data
              android:name="com.google.android.geo.API_KEY"
              android:value="@string/google_maps_key" /&gt;
  
          &lt;service android:name=".HelloService"&gt;&lt;/service&gt;
  
          &lt;receiver android:name="com.example.abc.project1.MyBroadcastreceiver" android:enabled="true" android:exported="false"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.PACKAGE_REPLACED" /&gt;
                  &lt;data android:scheme="package" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.PACKAGE_ADDED" /&gt;
                  &lt;data android:scheme="package" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>