Tuesday, 20 August 2013

Displaying progress bar while intent service is running

Displaying progress bar while intent service is running

This seems like it should be simple, but I'm getting into a muddle.
I start an IntentService from my main activity via an onClick event, and
listen for a response from the service using a PendingIntent that
references a static inner class BroadcastReceiver in the same main
activity as the onClick event, i.e.,
public void onClick(View arg0) {
Intent intent = new Intent(this, MyService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this,0,MyReceiver,0);
startService(intent);
}
public static class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Do stuff here ....
}
Now, all I want to do is show a ProgressBar (swirly indeterminate one) on
the activity's UI whilst the IntentService is doing its stuff. I defined
the ProgressBar in the xml as
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.Holo.ProgressBar.Large"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/progress"
android:visibility="gone"/>
I can start it from the onClick event by
ProgressBar pb = (ProgressBar) findViewById(R.id.progress);
pb.setVisibity(View.VISIBLE);
But I'm lost as to where and how to hide it??? I can't do it from
myReceiver as its a static class and I don't have access to the UI. It
seems so simple, but I can't figure out the best way. Any suggestions?
Thanks!

No comments:

Post a Comment