Android Progressbar not updating

前端 未结 5 1316
情深已故
情深已故 2021-01-28 22:33

i got a problem in updating progress bar. I am updating progress bar in separate Thread and the variable on which the progressbar progress is depending(which is a class variable

相关标签:
5条回答
  • 2021-01-28 23:13

    Try this.

    declare this variable at Global.

    int delay = 100;
    int period = 2000;
    

    Following function call where you need it.

    public void setProgressbar()
    {
        progressBar = new ProgressDialog(context);
        progressBar.setCancelable(true);
        progressBar.setMessage("File downloading ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();
    
        final Handler mhandler = new Handler();
    
        final Runnable mRunnable = new Runnable() {
    
            @Override
            public void run() {
    
                progressBar.setProgress(progress);
                progress++;
    
            }
        };
        final Timer timer = new Timer();
    
        timer.scheduleAtFixedRate(new TimerTask() {
    
            @Override
            public void run() {
    
                mhandler.post(mRunnable);
    
            }
        }, delay, period);
    }
    
    0 讨论(0)
  • 2021-01-28 23:15

    It's a Bug in ProgressBar!

    The setProgress(...) seems to not trigger the update on the drawable if the same value is passed again. But it's not triggered during the setMax, too. So the update is missing.

    To solve this, I'm just doing a bar.setProgress(0) before each update... this is only a workaround, but it works for me as expected:

    bar.setProgress(0); // call these two methods before setting progress.
    bar.setMax(20);
    bar.setProgress(20);
    

    Second Option.

    mSeekBar.post(new Runnable() {
            @Override
            public void run() {
                mSeekBar.setProgress(percentOfFullVolume);
            }
        });
    

    it may also work for someone.

    0 讨论(0)
  • 2021-01-28 23:16

    There are something wrong with your logic. Change your thread to following

    Thread thread = new Thread()
    {
        public void run()
        {
            int prog = 0;
             while(prog < 100)
             {
    
                 progressBar.setProgress(prog);
                 try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 prog ++;
             }
        }
    };
    thread.start();
    
    0 讨论(0)
  • 2021-01-28 23:21

    You are updating progress bar inisde a thread's run method. you cannot update ui from a thread. You need to update ui on the ui thread. Use runOnUiThread. runOnUiThread is a method of activity class.

    Or Use Asynctask.

    http://developer.android.com/reference/android/os/AsyncTask.html

    In asynctask doInBackground you can call publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

    0 讨论(0)
  • 2021-01-28 23:27

    Use AsyncTask, there is also an example in this document to answer your need.

    This code from AsyncTask actually do what you want to accomplish.

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }
    
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
    
     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
      }
    

    Only thing you need to change setProgressPercent() to your method name of progress bar value change. You could delete onPostExecute method AFAIK if you don't need it. Try to use AsyncTask instead of java Thread since AsyncTask provide methods already for your need while you need to write them if you use Thread instead.

    0 讨论(0)
提交回复
热议问题