Android Progressbar not updating

前端 未结 5 1419
情深已故
情深已故 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);
    }
    

提交回复
热议问题