I am using Firebase for creating a small chat application. I want ChildEventListener
to keep listening to the firebase database location though my app is in background or it is exited. Currently I am registering it and when my app exits or closed using finish()
, after that none of my methods of ChildEventListener are getting called like onChildAdded
or onChildChanged
though I haven't called removeEventListener
.I want ChildEventListener to be always running in background. Is there anyway of doing that?
Asked
Active
Viewed 4,677 times
10

Lalit Poptani
- 67,150
- 23
- 161
- 242
-
Hello, you found any solutions for this? – Hardik Joshi Jun 08 '17 at 07:27
-
No, but don't call `removeEventListener` and I guess it should work for you! – Lalit Poptani Jun 08 '17 at 11:26
-
Ok. You mean ChildEventListener will always work in background thread even app is kill OR we need to do all this stuff in service? – Hardik Joshi Jun 08 '17 at 11:56
-
I guess it will be killed when we kill app manually or the Android OS kills our app when resources are required, so we need to handle it manually! – Lalit Poptani Jun 08 '17 at 14:40
2 Answers
10
Use service to listen your ChildEventListener
public class ChildEventListener extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Adding a childevent listener to firebase
Firebase myFirebaseRef = new Firebase("FirebaseURL");
myFirebaseRef.child("FIREBASE_LOCATION").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//Do something using DataSnapshot say call Notification
}
@Override
public void onCancelled(FirebaseError error) {
Log.e("The read failed: ", error.getMessage());
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("The read failed: ", firebaseError.getMessage());
}
});
return START_STICKY;
}
}
register your Service inside Manifest
<service android:name=".ChildEventListener "/>
Start your Service and listen for childEvents, where/when to start your service is up to your chat app design

Frank van Puffelen
- 565,676
- 79
- 828
- 807

Raghavendra B
- 441
- 3
- 10
-
but when I will start my existing instance of service, then it will register the ChildEventListener again which is causing multiple listeners of ChildEventListener. Is there anyway to check that my ChildEventListener is already listening? – Lalit Poptani Jul 16 '16 at 16:28
-
Use sharedPreferences to check whether your childEvent listener is active or not.If it is not listening then only add that event listener – Raghavendra B Jul 16 '16 at 16:59
-
thats what I am saying, how to check that ChildEventListener? is there any listener that will tell me its not listening? – Lalit Poptani Jul 16 '16 at 17:27
-
why are you starting existing instance of Service again..If your service is running above code will listen for childEvent Listener.Using ActivityManger you can check whether your service is running or not otherwise you need start the Service – Raghavendra B Jul 16 '16 at 18:28
-
and what is the probability that if my service is running the ChildEventListener will also be running? – Lalit Poptani Jul 18 '16 at 05:25
-
what if service is killed if background data off or auto restart mode off and any other reason? during service off period i will miss data – Rajesh N Sep 30 '17 at 05:55