我是Firebase数据库的新手,但我知道Java是开发人员。我只想在我的应用程序未活动Internet连接时禁用Firebase数据库以获取数据。
我的应用程序类别是:
public class MApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FireBaseConnectionClass.setFirebaseDataBaseConnection();
}
}
和FireBaseConnectionClass类是:
public class FireBaseConnectionClass {
private static FirebaseDatabase firebaseDatabase = null;
public static void setFirebaseDataBaseConnection() {
MyMLogger.showMLog("version", "setting FB Connection:::", "indexactivity", "checkappversion");
if (firebaseDatabase == null) {
MyMLogger.showMLog("version", "connection was:::" + "null", "indexactivity", "checkappversion");
firebaseDatabase = FirebaseDatabase.getInstance();
firebaseDatabase.setPersistenceEnabled(false);
}//if ends
}
public static FirebaseDatabase getFirebaseDatabaseConnection() {
if (firebaseDatabase == null) {
setFirebaseDataBaseConnection();
}
return firebaseDatabase;
}
public static void onLineDataBase() {
if (firebaseDatabase != null) {
firebaseDatabase.goOnline();
}
}
public static void offLineDataBase() {
if (firebaseDatabase != null) {
firebaseDatabase.goOffline();
}
}
}
我只是从初始屏幕活动之后的活动中从数据库中获取应用程序版本!
private void checkAppVersion() {
try {
//activity not running return
if (IndexActivity.this.isFinishing()) {
return;
}
//get the database instance
FireBaseConnectionClass.getFirebaseDatabaseConnection().getReference("app_version").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//check if data snapshot exists
if (dataSnapshot.exists()) {
appVersionFromDatabase = dataSnapshot.getValue().toString();
MyMLogger.showMLog("version", "version:::" + appVersionFromDatabase, "indexactivity", "checkappversion");
} else {
MyMLogger.showMLog("version", "snapshot not exists", "indexactivity", "checkappversion");
FireBaseConnectionClass.offLineDataBase();
}// if (dataSnapshot.exists()) ends
}//onDataChange ends
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
MyMLogger.showMLog("version", "error:::" + databaseError.toString(), "indexactivity", "checkappversion");
FireBaseConnectionClass.offLineDataBase();
}
});
} catch (Exception exp) {
MyMLogger.showMLog("version", "exception:::" + exp.toString(), "indexactivity", "checkappversion");
}
}
主要问题是,当我关闭Internet时,checkAppVersion()方法给出的应用程序版本(可能来自缓存!)我不希望这样做! 我还遵循了official documentation。 有人可以告诉我为什么这种行为对我而言没有发生,因为我只有在具有活动的Internet连接时才需要该应用程序的版本!
预先感谢!