设备断开连接后如何更改Firebase数据?

时间:2019-09-15 17:20:51

标签: firebase firebase-realtime-database

我正在使用服务从后台更改Firebase中的数据。 我想通过更改互联网连接设置离线和在线状态。 如果设备具有连接Firebase /“ PrivacySetting” /“ userstaus” /“ online”。 如果设备丢失连接firebase /“ PrivacySetting” /“ userstaus” /“ offline”。 它的事实是,如果我的设备失去连接,那么我将如何更改Firebase数据。 因此,当设备断开连接时,是否还有其他方法可以更改Firebase数据?

public class NetworkBroadcast  extends Service {

static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
NotificationManager manager ;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (CONNECTIVITY_CHANGE_ACTION.equals(action)) {
                //check internet connection
                if (!ConnectionHelper.isConnectedOrConnecting(context)) {
                    if (context != null) {
                        boolean show = false;
                        if (ConnectionHelper.lastNoConnectionTs == -1) {//first time
                            show = true;
                            ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
                        } else {
                            if (System.currentTimeMillis() - ConnectionHelper.lastNoConnectionTs > 1000) {
                                show = true;
                                ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
                            }
                        }

                        if (show && ConnectionHelper.isOnline) {
                            ConnectionHelper.isOnline = false;
                            Log.i("NETWORK123","Connection lost");
                            ***//Here i want to set PrivacyRef.child(currentUserID).child("userstaus").setValue("offline");***
                        }
                    }
                }
                else
                {
                    Log.i("NETWORK123","Connected");
                    FirebaseAuth mAuth=FirebaseAuth.getInstance();
                    String currentUserID=mAuth.getCurrentUser().getUid();
                    DatabaseReference MesageRef=FirebaseDatabase.getInstance().getReference().child("Messages");
                    DatabaseReference PrivacyRef=FirebaseDatabase.getInstance().getReference().child("PrivacySetting");
                    PrivacyRef.child(currentUserID).child("userstaus").setValue("online");
                    ConnectionHelper.isOnline = true;
                }
            }
        }
    };
    registerReceiver(receiver,filter);
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}

}

ConnectionHelper代码:

public class ConnectionHelper {

    public static long lastNoConnectionTs = -1;
    public static boolean isOnline = true;

    public static boolean isConnected(Context context) {
        ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        return activeNetwork != null && activeNetwork.isConnected();
    }

    public static boolean isConnectedOrConnecting(Context context) {
        ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }
}

0 个答案:

没有答案