0

Goal: update a widget view after AsyncTask completes.

Scenario: widget onReceive receives an intent from a user click. This starts a networking operation in an AsyncTask. An interface has been passed to the AsyncTask via constructor as described here. In the interface method, I'm trying to send a broadcast that will trigger the widget's onUpdate method but I don't believe that it is firing.

Thoughts: My best guess is that onReceive has returned, therefore the Context that I was trying to use to sendBroadcast is no longer available. I was thinking that I could get() the AsyncTask but I don't want to trigger ANR if the operation takes too long. If I get(timeout, unit) the AsyncTask, I'm afraid that I risk missing the end of the task and the view will not be updated.

What's the best way to update the widget view upon completion of the task?

Solution: Turns out that I made a mistake crafting the Intent that I was broadcasting

Community
  • 1
  • 1
JB0x2D1
  • 838
  • 8
  • 20

1 Answers1

1

if you can hold a reference of the view to be updated,then just invoke view#onUpdate in defined interface.if not,and you want to use a Broadcast to inform the view to update,so I suggest you use the LocalBroadcastManager,then you don't need to worry if context exists now.Hope this could help you.

starkshang
  • 8,228
  • 6
  • 41
  • 52
  • +1 for trying. I had made a mistake creating the Intent that I was broadcasting, and that is why I never received it. – JB0x2D1 Sep 30 '15 at 15:11