title: How to use Firebase EventListener as a background service in android?
body: <p>I am trying to make a background service that runs even if the application is closed. this services is supposed to listen to Firebase changes, and start the application according to a trigger. I don't know whether I am missing something in the code or not even close to the right answer, but here is my code : </p>
  
  <pre><code>public class FireBaseService extends Service {
  
  private HashMap&lt;String, String&gt; fireBaseBattery;
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      fireBaseBattery = new HashMap&lt;&gt;();
      final Firebase firebaseRef_Battery = new Firebase("the url i want to take data from");
  
      firebaseRef_Battery.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              if (dataSnapshot.getValue() != null) {
                  fireBaseBattery = (HashMap&lt;String, String&gt;) dataSnapshot.getValue();
                  String battery = (String) fireBaseBattery.get("battery");
                  int battery_int = Integer.parseInt(battery);
  
                  System.out.println("SERVICE FIREBASE : " + battery);
                  if (battery_int &lt;= 10) {
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                      startActivity(intent);
                  }
              }
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
  
          }
      });
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And this is the manifest : </p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
      &lt;application
          android:name=".ParseApplication"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
  &lt;service android:enabled="true"
          android:name=".FireBaseService"&gt;
      &lt;/service&gt;
          &lt;activity
              android:name=".Launcher"
              android:label="@string/app_name"
              android:theme="@style/AppTheme.NoActionBar"&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;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Edit : I added a line in MainActivity.class to start the service</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      startService(new Intent(this, FireBaseService.class));
  }
  </code></pre>