How can Android update the current UI when it receives a push in BroadcastReceiver?
ringa_lee2017-05-24 11:37:50
0
3
1043
Use Xiaomi Push.
Then I received the information pushed to me by Xiaomi in the class integrated with BroadcastReceiver, but I want to update the unread readings on the UI at the same time. What should I do?
I don’t know if the unread information on the UI you are talking about refers to the icon in the upper right corner of the launcher or the unread information on the UI in the app. The former has nothing to do with the developer. Domestic manufacturers’ systems will automatically count the number of notifications received by the app. , if the latter is the case, perform corresponding operations by parsing the message in the onReceivePassThroughMessage(Context context, MiPushMessage message) method
There are many ways to update the number of messages, just choose the appropriate one. For example, it can be passively displayed: maintain a sharedPreference msgCout, get msgCount on the corresponding page and display it on the UI; or active display: you can register a broadcast on the interface where you want to display the number of messages, and send messages to this broadcast in the above code, or you can use EventBus Send messages and more
BroadcastReceiver doing time-consuming operations will cause ANR, and there is no way to directly modify the UI. In our project, RxBus is used to notify UI updates. Generally speaking, EventBus can be used, and the Sp mentioned above can also be used, but this You will not modify the UI directly, but you need to refresh it manually. If you use the MVP architecture, you can directly adjust the method update in the presenter
Bring the data and send a broadcast of your own app to the corresponding UI class that needs to be processed
I don’t know if the unread information on the UI you are talking about refers to the icon in the upper right corner of the launcher or the unread information on the UI in the app. The former has nothing to do with the developer. Domestic manufacturers’ systems will automatically count the number of notifications received by the app. , if the latter is the case, perform corresponding operations by parsing the message in the
onReceivePassThroughMessage(Context context, MiPushMessage message)
methodThere are many ways to update the number of messages, just choose the appropriate one. For example, it can be passively displayed: maintain a
sharedPreference msgCout
, get msgCount on the corresponding page and display it on the UI; or active display: you can register a broadcast on the interface where you want to display the number of messages, and send messages to this broadcast in the above code, or you can use EventBus Send messages and moreBroadcastReceiver doing time-consuming operations will cause ANR, and there is no way to directly modify the UI. In our project, RxBus is used to notify UI updates. Generally speaking, EventBus can be used, and the Sp mentioned above can also be used, but this You will not modify the UI directly, but you need to refresh it manually. If you use the MVP architecture, you can directly adjust the method update in the presenter