Why won't my progress bar increment properly?
I am trying to increment my progress bar on a broadcast recieved but the myProgressDialog.incrementProgressBy(increment);
code has no effect from my point of view. I have tried placing the command in different places but I still can see no effect at all.
Here is what my code looks like.
public static final int max = 180;
public final static int increment = (1/180);
@Override
public void onCreate(Bundle savedInstanceState) {
...
i = new Intent();
i.setAction(ITEM_CREATED);
registerReceiver(myBroadcastReceiver, new IntentFilter(ITEM_CREATED));
findFeeds = new Runnable(){
@Override
public void run()
{
getFeedObjects();
}
};
beginThread();
}
public static Context getAppContext() {
return context;
}
private void beginThread()
{
switch (checkConnectionState(OffsideLiteActivity.this))
{
case 0:
thread = new Thread(null, findItems, "DoingInBackground");
thread.start();
myProgressDialog = new ProgressDialog(this);
myProgressDialog.setCancelable(true);
myProgressDialog.setMessage("Loading...");
myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgressDialog.setProgress(0);
myProgressDialog.setMax(max);
myProgressDialog.show();
break;
case 1:
connectionError();
break;
default:
}
}
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
myProgressDialog.incrementProgressBy(increment);
Log.d("RECEIVED", "Broadcast received");
}
};
The interesting part is that logcat registers the Broadcast received
log that i placed after the myProgressDialog.incrementProgressBy(increment);
code
logcat
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109:开发者_如何学Python DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
Like I said I have tried putting the myProgressDialog.incrementProgressBy(increment);
command elsewhere in my code, but to no avail. Any suggestion?
The problem might be this line:
public final static int increment = (1/180);
I believe that int will be truncated to 0, so you're only ever incrementing the progress dialog by 0. You probably want increment to be 1.
Instead of using a broadcastreceiver to update your UI, couldn't you just use something like an obser-observable pattern, the listener would then run the UI update in the UI thread like this :
public void updateUIRequested( int increment )
{
runOnUIThread( new Runnable() { myProgressDialog.incrementProgressBy(increment); } );
}//met
or if you want something more modern, use an AsyncTask and still run the update in the UI Thread.
Regards, Stéphane
Try putting a debug point right at myProgressDialog.incrementProgressBy().... what is the value of increment? My guess is that it could be 0. Or change your Log to:
Log.d("RECEIVED", "Broadcast received, increment by " + increment);
精彩评论