Can NoticationManager.notify() be called from a worker thread?

前端 未结 1 400
长发绾君心
长发绾君心 2020-12-08 13:26

My question is more about what is a good practice than what is possible:

  • Is it a good thing to call NoticationManager.notify() from a work
相关标签:
1条回答
  • 2020-12-08 13:48

    It is acceptable to update a Notification from a worker thread because the Notification does not live in your application's process and hence you are not updating its UI directly. The Notification is maintained in a system process, and the Notification's UI is updated through RemoteViews (doc), which allows the manipulation of a view hierarchy that is maintained by a process other than your own. If you look at the source for Notification.Builder here you can see that it is ultimately building a RemoteViews.

    And if you look at the source for RemoteViews here you'll see that when you manipulate a view it is really just creating an Action (source) object and adding it to a queue to be processed. An Action is a Parcelable that is ultimately sent via IPC to the process that owns the Notification's view, where it can unpack the values and update the view as indicated... on it's own UI thread.

    I hope that clarifies why it is OK to update a Notification from a worker thread in your application.

    0 讨论(0)
提交回复
热议问题