简体   繁体   English

Android | 在收到推送通知时不断更新UI

[英]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. 我想在收到推送通知后立即更新UI,类似地,Facebook如何更新其通知计数。

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. 它在BroadcastReceiver的onReceive方法上更新。
  3. I open the activity (UI), the first time it gets the value and is displayed correctly. 我第一次打开活动(UI)时会获得该值并正确显示。

Problem: 问题:

I am in the activity (UI) and the notifications keep on coming, thus changing the value in the shared preferences. 我处于活动(UI)中,并且通知不断出现,因此更改了共享首选项中的值。 Now my UI is not updated until I move to another activity and come back. 现在,直到我移至另一个活动并返回之前,我的UI才会更新。 UI is created in onCreate method. UI是在onCreate方法中创建的。

Question: How can I update my UI continuously as soon as the notification arrives, without moving away from or changing my current activity (UI). 问题:如何在通知到达后立即连续更新UI,而又不离开或更改当前活动(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 . 当您存储数据的方法存在疑问时,您可以注册 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): 您可以按以下方式进行注册(假设您只是使用默认的共享prefs文件):

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. 您可以将您的活动注册为与当前班级相同的广播,使用sendOrderedBroadcast可以指定接收者获取广播消息的顺序。 Here you can put your Activity with a higher priority than the other class and 在这里,您可以将“活动”放在比其他班级更高的优先级上,

A) Update your shared preferences B) update your textview A)更新您的共享首选项B)更新您的文本视图

and then call to abortBroadcast, I'll give you a sample code: 然后调用abortBroadcast,我将为您提供示例代码:

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. 记住这一点,您必须在onResume方法中注册BroadcastReceiver ,而在onPause方法中取消注册。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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