Firebase实时数据库addChildEventListener无法正常工作

时间:2018-11-27 13:16:54

标签: android firebase firebase-realtime-database

我正在为我的工作使用服务,我的目标是我需要从服务器加载所有详细信息,并且在添加,更改或删除新内容时 所以我在服务内部使用以下代码

服务代码

public class ChildEventListenerNew extends Service {
public ChildEventListenerNew() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

Handler handler = new Handler();
int delay = 1000; //milliseconds

DatabaseReference firebaseDatabase;
int ct = 0, ct2 = 0;
boolean internet = false;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("product_table_milla", "onStartCommand " + "   is called   ct:" + ct);
    firebaseDatabase = FirebaseDatabase.getInstance().getReference("product_table");

    handler.postDelayed(new Runnable() {
        public void run() {
            //do something
            ct2++;
            if (isOnline() && internet == false) {
                internet = true;
                Log.i("product_table_milla", "handler " + "  isonline and internet is false   ct:" + ct2);

                setLinsier();

            } else if (isOnline() == false) {
                internet = false;
                Log.i("product_table_milla", "handler " + "  isoffline and internet is false   ct:" + ct2);


            }
            Log.i("product_table_milla", "handler " + "   is called   ct:" + ct2);
            handler.postDelayed(this, delay);
        }
    }, delay);


    //return super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

ChildEventListener childEventListener = null;

private void setLinsier() {
    if (childEventListener != null) {
        firebaseDatabase.removeEventListener(childEventListener);
    }
    childEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ct++;
            if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
                Log.i("product_table_milla", "onChildAdded " + dataSnapshot.getKey() + "  " + dataSnapshot.child("p_name").getValue(String.class) + "   ct:" + ct);
            } else {
                Log.i("product_table_milla", "onChildAdded is null");
            }
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ct++;
            if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
                Log.i("product_table_milla", "onChildChanged " + dataSnapshot.getKey() + "  " + dataSnapshot.child("p_name").getValue(String.class) + "   ct:" + ct);
            } else {
                Log.i("product_table_milla", "onChildChanged is null");
            }
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            ct++;
            if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
                Log.i("product_table_milla", "onChildRemoved " + dataSnapshot.getKey() + "  " + dataSnapshot.child("p_name").getValue(String.class) + "   ct:" + ct);
            } else {
                Log.i("product_table_milla", "onChildRemoved is null ");
            }

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ct++;
            if (dataSnapshot != null ? dataSnapshot.getValue() != null ? true : false : false) {
                Log.i("product_table_milla", "onChildMoved " + dataSnapshot.getKey() + "  " + dataSnapshot.child("p_name").getValue(String.class) + "   ct:" + ct);
            } else {
                Log.i("product_table_milla", "onChildMoved is null");
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            ct++;
            Log.i("product_table_milla", "onCancelled " + databaseError.getMessage() + "" + "   ct:" + ct);

        }
    };
    firebaseDatabase.addChildEventListener(childEventListener);

}

public boolean isOnline() {
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

    if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
        return false;
    }
    return true;
}}

我添加了此代码的清单文件

<service android:name=".services.ChildEventListenerNew">
        </service>

在我的主要活动中

if(isMyServiceRunning(ChildEventListenerNew.class)){

}else {
    startService(new Intent(getApplicationContext(),ChildEventListenerNew.class));
}

一切正常,但是当我禁用Internet连接并重新启用Internet连接时,addChildEventListener无法正常工作。但是代码“ postDelayed”可以正常工作,这意味着关闭我的应用程序后,互联网连接服务可以正常工作,但是firebaseDatabase.addChildEventListener没有响应。我尝试通过添加,更改和删除数据库中的详细信息,但是没有任何反应。 我的代码有什么问题?
请帮忙吗?
使用互联网连接时,我需要获取所有新添加的数据以及更改或删除的数据详细信息。 Firebase失效时间数据库代码有可能吗?

0 个答案:

没有答案