Firebase状态:
在Android上,Firebase自动管理连接状态以减少带宽和电池消耗。如果客户端没有活动的侦听器,没有挂起的写操作或onDisconnect操作,并且没有通过goOffline方法显式断开连接,则Firebase在闲置60秒后将关闭连接。
问题:即使60秒后仍打开互联网,.info/connected
也会显示“未连接”。
我的积极听众->
keepConnected = database.getReference().child(“keep_open”);
keepConnected.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.v(TAG,snapshot.getValue().toString());
}
@Override
public void onCancelled(DatabaseError error) { }
});
我的信息/已连接的侦听器->
DatabaseReference connectedRef =
FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
System.out.println("connected");
} else {
System.out.println("not connected");
}
}
@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});
Logs:
06-25 15:10:08.994 7572-7572/com.yourgesture.androidgesturecourier V/KEEPOPEN: 1529816684493
06-25 15:10:13.361 7572-7572/com.yourgesture.androidgesturecourier V/INTERNET: true
ONLINE
06-25 15:11:03.803 7572-7572/com.yourgesture.androidgesturecourier V/INTERNET: false
OFFLINE (edited)