我正在使用ValueEventListener
来检测firebase
中的数据更改。我遇到一种奇怪的情况,当数据成功更改后,我的应用程序能够在应用程序内添加和获取数据,但是即使发生数据更改,我的应用程序也无法从Firebase数据库读取数据(如果已将其安装在其他设备中)
在ValueEventListener
停止在其他设备的应用程序上响应之后,我的应用程序在安装的第一天就可以在两种设备上正常工作。
以下是将数据设置为firebase数据库的代码:
public class FirebaseSync extends Service {
FirebaseDatabase fd;
DatabaseReference drr;
@Override
public void onCreate() {
super.onCreate();
fd = FirebaseDatabase.getInstance();
LocalBroadcastManager.getInstance(FirebaseSync.this).registerReceiver(fReceive, new IntentFilter("FIRE"));
LocalBroadcastManager.getInstance(FirebaseSync.this).registerReceiver(fCity, new IntentFilter("FCITY"));
drr = fd.getReference("users").child("key").child("city");
drr.setValue("--");
}
private BroadcastReceiver fReceive = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null){
FireData fireData = new FireData(distance, speed, time, status);
DatabaseReference dr = fd.getReference("users").child("key");
dr.setValue(fireData, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
Log.i("FIREME","Value was set. Error = "+databaseError);
}
});
}
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(FirebaseSync.this).unregisterReceiver(fReceive);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
从数据库获取数据的代码:
public class Fire extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef, cref;
boolean connected;
ValueEventListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire);
connected = false;
cref = FirebaseDatabase.getInstance().getReference(".info/connected");
cref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue(Boolean.class) != null){
connected = dataSnapshot.getValue(Boolean.class);
if (connected){
firest.setText("Connected");
}else {
firest.setText("Disconnected");
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
firest.setText("Listener was cancelled");
}
});
}
@Override
protected void onStart() {
super.onStart();
myRef = database.getReference("users").child("key");
myRef.keepSynced(true);
listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
double di = dataSnapshot.child("dis").getValue(Double.class);
double sp = dataSnapshot.child("spe").getValue(Double.class);
String ti = dataSnapshot.child("ti").getValue(String.class);
boolean st = dataSnapshot.child("sta").getValue(Boolean.class);
String ci = dataSnapshot.child("cit").getValue(String.class);
distance.setText(String.format(getResources().getString(R.string.distT), dista, units));
speed.setText(String.format(getResources().getString(R.string.speedT), speeda));
time.setText(timea);
cityText.setText(cityN);
}catch (NullPointerException e){
e.printStackTrace();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Fire.this, "something wrong happened at read value", Toast.LENGTH_LONG).show();
}
};
myRef.addValueEventListener(listener);
}
@Override
protected void onStop() {
super.onStop();
if (myRef != null && listener != null){
myRef.removeEventListener(listener);
}
}
}
我还在firebase控制台上添加了SHA1调试和发布键,但也没有解决。
数据库权限:
{
"rules": {
".read": true,
".write": true
}
}
App Gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.adarsh.epoch"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguardandroid.txt'),'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint- layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.0.0'
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.4'
}
已经一个星期了,我没有任何解决办法。