即使我的firebase数据没有变化,onStartCommand()方法也会多次调用

时间:2016-06-27 15:38:43

标签: android firebase

我是Android的新手,我正在尝试使用Firebase概念。我在设备注册后调用了startService方法。

这是我的RegistrationActivity代码

if (status.trim().equalsIgnoreCase("success")) {
                                    //Displaying a success toast
                                    Toast.makeText(RegistrationActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();

                                    //Opening shared preference
                                    SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);

                                    //Opening the shared preferences editor to save values
                                    SharedPreferences.Editor editor = sharedPreferences.edit();

                                    //Storing the unique id
                                    editor.putString(Constants.UNIQUE_ID, uniqueId);

                                    //Saving the boolean as true i.e. the device is registered
                                    editor.putBoolean(Constants.REGISTERED, true);

                                    //Applying the changes on sharedpreferences
                                    editor.apply();

                                    Log.d("NotificationIntent","Registration success");
                                    //Starting our listener service once the device is registered
                                    startService(new Intent(getBaseContext(), NotificationListenerService.class));

}

这是我的NotificationListenerService

public class NotificationListenerService extends Service {

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //When the service is started
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("NotificationListenerService","onStartCommand");
        //Opening sharedpreferences
        SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);

        //Getting the firebase id from sharedpreferences
        String id = sharedPreferences.getString(Constants.UNIQUE_ID, null);

        //Creating a firebase object
        Firebase firebase = new Firebase(Constants.FIREBASE_APP + id);

        //Adding a valueevent listener to firebase
        //this will help us to  track the value changes on firebase
        firebase.addValueEventListener(new ValueEventListener() {

            //This method is called whenever we change the value in firebase
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                Log.d("NotificationListenerService","onDataChange");
                //Getting the value from firebase
                //We stored none as a initial value
                String msg = snapshot.child("msg").getValue().toString();

                //So if the value is none we will not create any notification
                if (msg.equals("none"))
                    return;

                //If the value is anything other than none that means a notification has arrived
                //calling the method to show notification
                //String msg is containing the msg that has to be shown with the notification



                    showNotification(msg);

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("The read failed: ", firebaseError.getMessage());
            }
        });

        return START_STICKY;
    }

当firebase值没有变化时,通知会在app关闭时通过调用onStartCommand多次获得。

1 个答案:

答案 0 :(得分:0)

ValueListener.onDataChange()会触发数据更改以及when the listener is added。因此,每次调用onStartCommand()并且msg不是“无”时,您都会收到通知。

由于您的onStartCommand()返回START_STICKY,如果您的应用程序在后台并且系统会因为内存压力而导致服务中断,则会在以后重新启动您的服务,从而导致另一个通知张贴。