如何在android中刷新数据库引用

时间:2018-05-16 12:11:45

标签: java android firebase firebase-realtime-database

代码

public class IncomingCall extends BroadcastReceiver {
    //private AppPhoneStateListener phoneStateListener;
    private TelephonyManager telephonyManager;
    private HashMap<String, AppPhoneStateListener> appPhoneStateListenerHashMap = new HashMap<>();
@Override
public void onReceive(Context context, Intent intent) {
    try {
        // Create manager for listener registration
        AppPhoneStateListener phoneStateListener = new AppPhoneStateListener();

        telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        // Register listener for LISTEN_CALL_STATE
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Crashlytics.logException(e);
    }
}


private class AppPhoneStateListener extends PhoneStateListener {
    private CallMonitoring callMonitoring = new CallMonitoring();

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // Service only for child device
        Role role = User.getRole();
        if (role == Role.parent) return;

        Log.d("MyPhoneListener","state [" + state + "]");

        if (state == TelephonyManager.CALL_STATE_RINGING) {
            callMonitoring.registerChangeState(state);
            if (!appPhoneStateListenerHashMap.containsKey(incomingNumber)) {
                appPhoneStateListenerHashMap.put(incomingNumber, this);
            } else {
                telephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
            }
            //Log.d("MyPhoneListener","saved call states [" + callMonitoring.showCallStates() + "]");
        }
        if (appPhoneStateListenerHashMap.containsKey(incomingNumber)) {
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                callMonitoring.registerChangeState(state);
                //Log.d("MyPhoneListener","saved call states [" + callMonitoring.showCallStates() + "]");
            }
            if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Log.d("MyPhoneListener","saved call states [" + callMonitoring.showCallStates() + "]");
                // There was incoming call then we have call attachCall api method
                if (callMonitoring.wasIncomingCall()) {
                    long duration = callMonitoring.getLastActionDuration();
                    CallType callType = callMonitoring.getCallType();
                    CallStatus callStatus = callMonitoring.getCallStatus();
                    // Logging to debug
                    Log.d("MyPhoneListener","api params: phone [" + incomingNumber + "]; duration [" + duration + "]; callType [" + callType.getCallType() + "]; callStatus [" + callStatus.getCallStatus() + "]");
                    // Call api

                    // Unregister listener for LISTEN_CALL_STATE
                    unsubscribeCallListener(incomingNumber);
                }
                // Return call monitoring call to initial state
                callMonitoring.clearState();
            }
        }
    }

    private void unsubscribeCallListener(String incomingNumber) {
        appPhoneStateListenerHashMap.remove(incomingNumber);
        telephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
    }
}

} 问题编号不断增加所以我需要刷新数据库引用以获取数据库中的下一个问题...我如何实现这一点...到目前为止代码正在按预期工作但我不确定如何刷新.. .................................................. .................................................. ................................................. < / p>

1 个答案:

答案 0 :(得分:1)

为了迭代从Firebase获得的值中的值,您需要一个DataSnapshot迭代器。

以下是一个例子:

mDatabaseReference.child("Users").child(RecieversId).child("Quiz").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot snapshot: dataSnapshot.getChildren()){
            question = snapshot.child("Question").getValue(String.class);
            option1 = dataSnapshot.child("Option1").getValue(String.class);
            //Do bla bla...
        }
    }
}

通过此快照迭代器,您可以遍历Firebase数据库中的值。在做bla bla .. 部分,你可以完成剩下的工作。