title: Android: Service crashed after closing the app
body: <p>I've just started learning writing a Service yesterday, and here I have a service, which I expect to run all the time even if my App terminates. This service has a listener on my Firebase database and generates Notifications if the data has changed.</p>
  
  <p>What confused me was that my service could run successfully ONLY IF the app was running. Immediately after I closed the app, my phone alerted me that "Unfortunately, this app has stopped (crashed)". The alert came two times and then the service never restarted (I guess).</p>
  
  <p>I couldn't figure out why my service crashed after closing the App. I started the Service in the OnCreate method in MainActivity. And here's the whole service class:</p>
  
  <pre><code>public class MyService extends Service
  {
  private static DatabaseReference databaseReference;
  private static String uid;
  private static SharedPreferences sharedPreferences;
  private static int notification_id=0;
  private static boolean launched=false;
  public MyService(){}
  @Override public int onStartCommand(Intent intent, int flags, int startId)
  {
      if(!launched)
      {
          launched=true;
          databaseReference=MainActivity.databaseReference;
          uid=MainActivity.uid;
          sharedPreferences=MainActivity.sharedPreferences;
          databaseReference.child("users").child(uid).addValueEventListener(new ValueEventListener()
          {
              @Override public void onDataChange(DataSnapshot dataSnapshot)
              {
                  MyData myData=dataSnapshot.getValue(MyData.class);
                  if(myData!=null)
                  {
                      ArrayList&lt;String&gt; myGroups=myData.getGroups();
                      for(String group_id:myGroups)
                      {
                          if(group_record_listener!=null)databaseReference.child("group_records").child(group_id).removeEventListener(group_record_listener);
                          databaseReference.child("group_records").child(group_id).addValueEventListener(group_record_listener);
                      }
                  }
              }
              @Override public void onCancelled(DatabaseError databaseError)
              {
                  System.out.println(databaseError.getMessage());
              }
          });
      }
      return START_STICKY;
  }
  private ValueEventListener group_record_listener=new ValueEventListener()
  {
      @Override public void onDataChange(DataSnapshot dataSnapshot)
      {
          final String group_id=dataSnapshot.getKey();
          Query lastQuery=databaseReference.child("group_records").child(group_id).child("records").orderByKey().limitToLast(1);
          lastQuery.addListenerForSingleValueEvent(new ValueEventListener()
          {
              @Override public void onDataChange(DataSnapshot dataSnapshot)
              {
                  for(DataSnapshot childSnapshot:dataSnapshot.getChildren())
                  {
                      final Record new_record=childSnapshot.getValue(Record.class);
                      if(new_record!=null)
                      {
                          if((new_record.getType()==1)&amp;&amp;new_record.getTo_name().equals(sharedPreferences.getString("name","")))
                          {
                              if(group_record_listener!=null)databaseReference.child("group_records").child(group_id).removeEventListener(group_record_listener);
                          }
                          if(!new_record.getOperator().equals(sharedPreferences.getString("name","")))
                          {
                              databaseReference.child("groups").child(group_id).child("name").addListenerForSingleValueEvent(new ValueEventListener()
                              {
                                  @Override public void onDataChange(DataSnapshot dataSnapshot)
                                  {
                                      String group_name=(String)dataSnapshot.getValue();
                                      generateNotification(new_record,group_name);
                                  }
                                  @Override public void onCancelled(DatabaseError databaseError){}
                              });
                          }
                      }
                  }
              }
              @Override public void onCancelled(DatabaseError databaseError){}
          });
      }
      @Override public void onCancelled(DatabaseError databaseError)
      {
          System.out.println(databaseError.getMessage());
      }
  };
  private void generateNotification(Record record,String group_name)
  {
      String str="";
      switch (record.getType())
      {
          //. . . determine str
      }
      Notification notification=new Notification.Builder(this)
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentTitle("group \""+group_name+"\"")
              .setContentText(str)
              .setWhen(System.currentTimeMillis())
              .build();
      NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      notification_id++;
      manager.notify(notification_id,notification);
  }
  @Override public IBinder onBind(Intent intent)
  {
      return null;
  }
  }
  </code></pre>
  
  <p>If you have any idea, please help. Thanks in advance~~</p>