0

Is there a way to distinguish when there is no Internet connection in the Android Firebase Realtime Database?

Michael
  • 5
  • 2
  • To check whether a user has internet connectivity, or not, please check the duplicate to see the solution. – Alex Mamo Jun 28 '21 at 16:22

1 Answers1

0

I'm assuming you want to check if user is connected to database or so. To check that Firebase has a special reference which you can use to detect connection state:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        boolean connected = snapshot.getValue(Boolean.class);
        if (connected) {
            Log.d(TAG, "connected");
        } else {
            Log.d(TAG, "not connected");
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.w(TAG, "Listener was cancelled");
    }
});

You can read more about it in the documentation

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84