进度条倒数计时器

时间:2018-07-16 15:13:22

标签: java android android-asynctask android-progressbar countdowntimer

我正在尝试使用AsyncTask和CountDownTimer更新ProgressBar。我没有任何错误,但是ProgressBar没有更新/不起作用。

由于myDownloadTask扩展了AsyncTask,因此我不需要publish方法或onProgressUpdate()。我的ProgressBar在doInBackground()中自动更新。

我认为CountDownTimer中的代码未执行,但是我接下来编写了“ monCountTimer.start();”类的语句。

这是我的Java代码:

package com.example.alexandre.monprojperso;

import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.widget.ProgressBar;
import android.widget.Toast;



public class myDownloadTask extends AsyncTask<Integer, Integer, String> {
    public void setMonCountTimer(CountDownTimer monCountTimer) {
        this.monCountTimer = monCountTimer;
    }

    public void setInc(int inc) {
        this.inc = inc;
    }

    public void setMaProgressBar(ProgressBar maProgressBar) {
        this.maProgressBar = maProgressBar;
    }

    public CountDownTimer getMonCountTimer() {

        return monCountTimer;
    }

    public int getInc() {
        return inc;
    }

    public ProgressBar getMaProgressBar() {
        return maProgressBar;
    }

    protected CountDownTimer monCountTimer;
    private int inc = 0;
    protected ProgressBar maProgressBar;

    public void setMonAct(MainActivity monAct) {
        this.monAct = monAct;
    }

    public MainActivity getMonAct() {
        return monAct;
    }

    private MainActivity monAct;


    public myDownloadTask(ProgressBar maBarre, MainActivity monActi) {
        this.maProgressBar = maBarre;
        this.monAct = monActi;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.maProgressBar.setProgress(0);

    }

    @Override
    protected void onProgressUpdate(Integer... integers) {
        super.onProgressUpdate(integers);

    }

    @Override
    protected String doInBackground(Integer... integers) {
        Thread monThread = new Thread(new Runnable() {
            @Override
            public void run() {
                monCountTimer = new CountDownTimer(6000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        myDownloadTask.this.inc++;
                        myDownloadTask.this.getMaProgressBar().setProgress((int) myDownloadTask.this.inc* 100 / (6000 / 1000));
                    }
                    @Override
                    public void onFinish() {
                    }

                };
                monCountTimer.start();
            }
        });
        return "coucou";
    }
}

主要类别:

package com.example.alexandre.monprojperso;

import android.media.tv.TvContract;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.concurrent.atomic.AtomicBoolean;

public class MainActivity extends AppCompatActivity  {

    private ProgressBar maBarre ;

    public void setMaBarre(ProgressBar maBarre) {
        this.maBarre = maBarre;
    }

    public void setLaunchAsync(Button launchAsync) {
        this.launchAsync = launchAsync;
    }

    public void setMonHandler(Handler monHandler) {
        this.monHandler = monHandler;
    }

    public void setMonCountTimer(CountDownTimer monCountTimer) {
        this.monCountTimer = monCountTimer;
    }

    public void setMonTask(myDownloadTask monTask) {
        this.monTask = monTask;
    }

    public void setProgressThread(Thread progressThread) {
        ProgressThread = progressThread;
    }

    public void setI(int i) {

        this.i = i;
    }

    public ProgressBar getMaBarre() {

        return maBarre;
    }

    public Button getLaunchAsync() {
        return launchAsync;
    }

    public Handler getMonHandler() {
        return monHandler;
    }

    public CountDownTimer getMonCountTimer() {
        return monCountTimer;
    }

    public String getPROGRESS_BAR_INCREMENT() {
        return PROGRESS_BAR_INCREMENT;
    }

    public static int getMessagePreExecute() {
        return MESSAGE_PRE_EXECUTE;
    }

    public static int getMessageProgressUpdate() {
        return MESSAGE_PROGRESS_UPDATE;
    }

    public static int getMessagePostExecute() {
        return MESSAGE_POST_EXECUTE;
    }

    public myDownloadTask getMonTask() {
        return monTask;
    }

    public Thread getProgressThread() {
        return ProgressThread;
    }

    public int getI() {
        return i;
    }

    private Button launchAsync;
    private Handler monHandler ;
    private CountDownTimer monCountTimer ;

    private final String PROGRESS_BAR_INCREMENT="ProgreesBarIncrementId";
    private static final int MESSAGE_PRE_EXECUTE = 1;
    private static final int MESSAGE_PROGRESS_UPDATE = 2;
    private static final int MESSAGE_POST_EXECUTE = 3;
    myDownloadTask monTask;
    Thread ProgressThread ;
    private int i = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        maBarre = (ProgressBar) findViewById(R.id.progressBar);
        launchAsync = (Button) findViewById(R.id.button);
        setSupportActionBar(toolbar);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        this.launchAsync.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                monTask = new myDownloadTask(MainActivity.this.maBarre , MainActivity.this);
                monTask.execute();

                }
        });

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
    }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

}

能帮我吗?

4 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />





</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="@layout/activity_main">

            <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="236dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="@+id/progressBar"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/progressBar"
            app:layout_constraintTop_toTopOf="parent" />

            <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginBottom="336dp"
            android:layout_marginEnd="104dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    <android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

为您的XML文件尝试此操作。我不知道您要实现哪种布局,但是当我执行“ setProgress(50);”时在onCreate中,使用发布的XML文件没有任何显示。

我创建了这个XML布局,现在进度条看起来很好:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="140dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="10dip"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.constraint.ConstraintLayout>

在onCreate中,添加:

ProgressBar bar = findViewById(R.id.progressBar);
bar.setProgress(50);

测试布局...

其他一些问题:已经创建了线程,但之后必须调用start()。

 monCountTimer.start();

在public void run()中,代码正在执行以下操作:

  myDownloadTask.this.getMaProgressBar().setProgress((int) 
    myDownloadTask.this.inc* 100 / (6000 / 1000));

从不更新Android中辅助线程的视图。所有视图都必须在“主线程”(也称为“ UI线程”)上进行更新

解决此问题的快速方法是:

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });

我已经进行了上面的更改,现在可以正常运行了:

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ProgressBar;


public class MainActivity extends AppCompatActivity  {

    private ProgressBar maBarre ;

    public void setMaBarre(ProgressBar maBarre) {
        this.maBarre = maBarre;
    }

    public void setLaunchAsync(Button launchAsync) {
        this.launchAsync = launchAsync;
    }

    public void setMonHandler(Handler monHandler) {
        this.monHandler = monHandler;
    }

    public void setMonCountTimer(CountDownTimer monCountTimer) {
        this.monCountTimer = monCountTimer;
    }

    public void setMonTask(myDownloadTask monTask) {
        this.monTask = monTask;
    }

    public void setProgressThread(Thread progressThread) {
        ProgressThread = progressThread;
    }

    public void setI(int i) {

        this.i = i;
    }

    public ProgressBar getMaBarre() {

        return maBarre;
    }

    public Button getLaunchAsync() {
        return launchAsync;
    }

    public Handler getMonHandler() {
        return monHandler;
    }

    public CountDownTimer getMonCountTimer() {
        return monCountTimer;
    }

    public String getPROGRESS_BAR_INCREMENT() {
        return PROGRESS_BAR_INCREMENT;
    }

    public static int getMessagePreExecute() {
        return MESSAGE_PRE_EXECUTE;
    }

    public static int getMessageProgressUpdate() {
        return MESSAGE_PROGRESS_UPDATE;
    }

    public static int getMessagePostExecute() {
        return MESSAGE_POST_EXECUTE;
    }

    public myDownloadTask getMonTask() {
        return monTask;
    }

    public Thread getProgressThread() {
        return ProgressThread;
    }

    public int getI() {
        return i;
    }

    private Button launchAsync;
    private Handler monHandler ;
    private CountDownTimer monCountTimer ;

    private final String PROGRESS_BAR_INCREMENT="ProgreesBarIncrementId";
    private static final int MESSAGE_PRE_EXECUTE = 1;
    private static final int MESSAGE_PROGRESS_UPDATE = 2;
    private static final int MESSAGE_POST_EXECUTE = 3;
    myDownloadTask monTask;
    Thread ProgressThread ;
    private int i = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        maBarre = (ProgressBar) findViewById(R.id.progressBar);
        launchAsync = (Button) findViewById(R.id.button);
        //etSupportActionBar(toolbar);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        this.launchAsync.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                monTask = new myDownloadTask(MainActivity.this.maBarre , MainActivity.this);
                monTask.execute();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        /*
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        */

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }


    public class myDownloadTask extends AsyncTask<Integer, Integer, String> {
        public void setMonCountTimer(CountDownTimer monCountTimer) {
            this.monCountTimer = monCountTimer;
        }

        public void setInc(int inc) {
            this.inc = inc;
        }

        public void setMaProgressBar(ProgressBar maProgressBar) {
            this.maProgressBar = maProgressBar;
        }

        public CountDownTimer getMonCountTimer() {

            return monCountTimer;
        }

        public int getInc() {
            return inc;
        }

        public ProgressBar getMaProgressBar() {
            return maProgressBar;
        }

        protected CountDownTimer monCountTimer;
        private int inc = 0;
        protected ProgressBar maProgressBar;

        public void setMonAct(MainActivity monAct) {
            this.monAct = monAct;
        }

        public MainActivity getMonAct() {
            return monAct;
        }

        private MainActivity monAct;


        public myDownloadTask(ProgressBar maBarre, MainActivity monActi) {
            this.maProgressBar = maBarre;
            this.monAct = monActi;
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            this.maProgressBar.setProgress(0);

        }

        @Override
        protected void onProgressUpdate(Integer... integers) {
            super.onProgressUpdate(integers);

        }

        @Override
        protected String doInBackground(Integer... integers) {
            Thread monThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            monCountTimer = new CountDownTimer(6000, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    myDownloadTask.this.inc++;
                                    myDownloadTask.this.getMaProgressBar().setProgress((int) myDownloadTask.this.inc * 100 / (6000 / 1000));
                                }

                                @Override
                                public void onFinish() {
                                }

                            };
                            monCountTimer.start();

                        }
                    });
                }
            });
            monThread.start();
            return "coucou";
        }
    }
}

一些要考虑的问题:

  • 在这里创建的线程超出了需要的数量,可以更简单地编写代码。 doInBackground已经在一个单独的线程上,因此无需在其中创建另一个线程。另外,我相信CountDownTimer已经在其自己的线程中运行,因此这里很多代码都是不必要的。
  • 完成开发后,请摆脱所有这些警告。软件开发实际上是关于纪律和习惯的,只有不好的开发人员才忽略警告!完成后,进行“分析->检查代码”。

但是您的进度条更新程序现在对我来说很好用。

答案 3 :(得分:0)

好,谢谢!!!!:)

我尝试过:

by(df, df$Check, function(x) all(c('A', 'B') %in% x$Item))

使用此代码,我的应用程序刚刚崩溃。

但是使用 @Override protected String doInBackground(Integer... integers) { Thread monThread = new Thread(new Runnable() { @Override public void run() { monCountTimer = new CountDownTimer(6000, 1000) { @Override public void onTick(long millisUntilFinished) { myDownloadTask.this.inc++; myDownloadTask.this.getMaProgressBar().setProgress((int) myDownloadTask.this.inc* 100 / (6000 / 1000)); } @Override public void onFinish() { } }; monCountTimer.start(); } monThread.start(); }); return "coucou"; } 可以正常工作

我们使用2个线程...;(未优化,而monCountTimer使用3个,也许,我们可以删除runOnUiThread(new Runnable() {....}

“我相信CountDownTimer已经在其自己的线程中运行,因此这里很多代码都是不必要的。”

我尝试过:

monThread

它不起作用