Skip to content Skip to sidebar Skip to footer

Unable To Update Ui From Intentservice

A seemingly simple requirement - to update UI from an IntentService. In the shot below, when the Start Service button is clicked, I need to show a ProgressBar above the only TextVi

Solution 1:

First, you are tying up the main application thread for ~2.5 seconds. This will freeze your UI during this period of time. Do not do this.

Second, you are calling updateUI() once before the ~2.5 seconds, and once after. Since you are tying up the main application thread during that time, this will have the same visual effect as calling updateUI() twice in succession after the delay. updateUI() toggles the ProgressBar visibility, so two calls will cancel each other out, leaving you in the same state as you started.

I need to show a ProgressBar above the only TextView and after a delay, remove the ProgressBar.

Showing a ProgressBar for 2.5 seconds, irrespective of any actual work being done, is rather arbitrary.

That being said, call updateUI() once, then use pb.postDelayed() to schedule a Runnable to run 2500 milliseconds later, where the run() method in the Runnable calls updateUI() the second time. This avoids you blocking the main application thread, so it allows the first updateUI() call to take effect, while still giving you the 2.5-second duration.

Solution 2:

Thanks so much, @CommonsWare! Finally got it to work. It needed two receivers for the two different actions, which I discovered here.

For the benefit of those looking for the final working code...

Result screens:- after clicking and after processing

Pardon any copy-paste ghosts in the code, it is after all a PoC, albeit functional.

Post a Comment for "Unable To Update Ui From Intentservice"