开发者

Android Progress bars losing their value when updated in a Handler

I am working on an Android app that displays cell phone usage information in a progress bar. The bar changes color based on the amount of usage from green to yellow to red. When my TimerTask executes the update though (via a Handler so it goes through the UI thread and not the Timer thread), the progress bars empty, even though the text labels are updated correctly. The code updating the progress bar is:

private void SetBarColor(ProgressBar bar, int progress, int secondaryAdditive){
    int setTo = R.drawable.greenbar;

    if(progress < 60)
        setTo = R.drawable.greenbar;
    else if (progress < 90)
        setTo = R.drawable.yellowbar;
    else
        setTo = setTo = R.drawable.redbar;

    bar.setProgress(progress);
    bar.setSecondaryProgress(progress + secondaryAdditive); //Mocking up phone usage

    Rect bounds = bar.getProgressDrawable().getBounds();
    bar.setProgressDrawable(this.getResources().getDrawable(setTo));
    bar.getProgressDrawable().setBounds(bounds);
    bar.setVisibility(View.VISIBLE);
}

This method works fine when called the first time in the onCreate method, but when called from the TimerTask, the bars simply hide themselves, showing only the grey background (as if their progress == 0). I've used the debugger and confirmed that the right values are going into the setProgress and setSecondaryProgress() calls. I have also tried setting the progress both before (as in the snippet above) and after the setProgressDrawable call, to no avail.

Anyone run into something like this?

EDIT: By request, some additional code. Here's the Runnable:

private class MyTime extends TimerTask {         
    @Override
    public void run() {
        ReQueryCount--;
        if(ReQueryCount <= 0){
            ReQueryCount = ReQueryCountStarter;

            HeartbeatHandler.post(new Runnable() {
开发者_C百科                public void run() {
                    GetDataFromServer();
                }
            });

        }
    }
}

The HeartbeatHandler is created in onCreate.

GetDataFromServer gets some data from server, but the part that consumes my SetBarColor above is:

private void UpdateProgressBars(ServiceHelper.UsageResult result) {

    int voiceBarProg = (int)((double)result.VoiceUsage / (double)result.MaxVoice * 100);
    int dataBarProg = (int)((double)result.DataUsage / (double)result.MaxData * 100);
    int msgBarProg = (int)((double)result.TextUsage / (double)result.MaxText * 100);

    SetBarColor(voiceBar, voiceBarProg, PhoneVoice);
    SetBarColor(dataBar, dataBarProg, PhoneData);
    SetBarColor(msgBar, msgBarProg, PhoneText);
}

Short of posting the layouts, manifest and the rest I'm not sure what other code would be helpful.


After searching for hours for a solution of this problem I have found this article that has an answer that has helped me.

But in addition to the answer there I have also get/set the drawable bounds too. So I have a custom ProgressBar which has the following method to set progress color and amount:

    public void setProgressColorAndFill(int color, int fill){
    Drawable bgDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xFFdadada, 0xFFdadada});
    Drawable proDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{color, color});
    ClipDrawable clip = new ClipDrawable(proDrawable, Gravity.LEFT,ClipDrawable.HORIZONTAL);

    Drawable[] layers = new Drawable[2];
    layers[0] = bgDrawable;
    layers[1] = clip;

    LayerDrawable layerDrawable = new LayerDrawable(layers);
    layerDrawable.setId(0, android.R.id.background);
    layerDrawable.setId(1, android.R.id.progress);

    Rect bounds = ((LayerDrawable)getProgressDrawable()).getBounds();
    setProgressDrawable(layerDrawable);
    getProgressDrawable().setBounds(bounds);

    setProgress(1);
    setMax(100);
    setProgress(fill);
}

Hope this will be useful to someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜