简体   繁体   中英

Android | Continuously update UI on receiving a push notification

I want to update my UI as soon as I receive the push notification, similarly how facebook updates its notification count.

What I have achieved:

  1. I get the notification.
  2. I save the value in the shared preferences. This is continuously updating, as soon as I get a new notification, my value in the shared preferences is updated. It is updated on onReceive method of the BroadcastReceiver.
  3. I open the activity (UI), the first time it gets the value and is displayed correctly.

Problem:

I am in the activity (UI) and the notifications keep on coming, thus changing the value in the shared preferences. Now my UI is not updated until I move to another activity and come back. UI is created in onCreate method.

Question: How can I update my UI continuously as soon as the notification arrives, without moving away from or changing my current activity (UI).

Thanks

如果您在共享首选项中存储值,则建议不要在活动中使用OnSharedPreferenceChangeListener http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html

While your method of storing the data is questionable, you can register an OnSharedPreferenceChangeListener . This will receive a callback when your preferences are changed. Note that it must be an instance variable and not an inner anonymous class.

private OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener() {
    @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        updateNotificationCount();
    }
};

You can register this as follows (assuming you're just using the default shared prefs file):

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPrefs(context);
sharedPrefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener);

You could register your activity to the same broadcast as the class you have right now, using sendOrderedBroadcast you can specify the order your receivers will get the broadcasted message. Here you can put your Activity with a higher priority than the other class and

A) Update your shared preferences B) update your textview

and then call to abortBroadcast, I'll give you a sample code:

The class that sends the broadcast:

Intent intent = new Intent();
intent.setAction("increase action");
intent.putExtra("new value");
context.sendOrderedBroadcast(intent,null);

Your activity

First you have to create a broadcastreceiver

private class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       //Do your work
       abortBroadcast();
  }
}

And then register it

IntentFilter filterSync = new IntentFilter();
filterSync.addAction("increase action");
filterSync.setPriority(100);
registerReceiver(myregister, filterSync);

Remember this, you have to register your BroadcastReceiver in your onResume method and unregister it in your onPause method.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM