Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道firebase中的值是增加了还是减少了?_Java_Android_Firebase_Service_Firebase Realtime Database - Fatal编程技术网

Java 如何知道firebase中的值是增加了还是减少了?

Java 如何知道firebase中的值是增加了还是减少了?,java,android,firebase,service,firebase-realtime-database,Java,Android,Firebase,Service,Firebase Realtime Database,我正在开发一个android应用程序,其中有一个选项可以跟随/不跟随用户。每当有人跟踪他时,FirebaseDatabase中的计数器将增加1,而当有人松开他时,计数器将减少1 我试图在每次有人跟踪用户时通知他,只要用户离开应用程序,我就启动一项服务,并在服务中写入代码 以下是服务的onCreate()中的代码: firebaseDatabaseFollowers.child("***").child("***").child("followers").addValueEventList

我正在开发一个android应用程序,其中有一个选项可以跟随/不跟随用户。每当有人跟踪他时,
FirebaseDatabase
中的计数器将增加1,而当有人松开他时,计数器将减少1

我试图在每次有人跟踪用户时通知他,只要用户离开应用程序,我就启动一项服务,并在
服务中写入代码

以下是服务的
onCreate()
中的代码:

    firebaseDatabaseFollowers.child("***").child("***").child("followers").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                b = sharedPref2.getBoolean(SettingsActivity.KEY_PREF_NOTIF_FOLLOW, true);
                Toast.makeText(getBaseContext(), "b: " + String.valueOf(b), Toast.LENGTH_SHORT).show();

                if (dataSnapshot.getValue() != null) {

                    if (String.valueOf(b).equals("true")) {

                        final int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

                        Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
                        notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationService());
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);

                    }
                } else {
                    Toast.makeText(getBaseContext(), "database is null", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
通知部分工作正常。唯一的问题是,即使有人解除了对用户的跟踪,也会发送通知,我不希望这种情况发生

那么,有没有办法知道如果
FirebaseDatabase
中的计数器增加或减少,我可以相应地通知您


请让我知道。

我会在你的应用程序中存储一个全局变量,该变量包含用户当前拥有的追随者数量。当数据库发送新信息时,您可以使用if new number>或我是否应该将“保存用户当前拥有的追随者数量的全局变量”保存在
SharedReferences
中?我不使用Android编程,所以我不知道这是什么意思-抱歉!我想更合理的做法是保存变量,以便可以在您需要的任何类中访问它(public和private)