Nik*_*iki 3 android asynchronous android-progressbar android-asynctask progress-bar
在我的应用程序中,我正在下载视频文件.我想显示下载的进度条.
如何通过AsyncTask Concept实现?
谢谢,
尼基
使用AsyncTask的onProgressUpdate方法.
如果您知道文件的大小,可以在onPreExecute中设置最大值:
protected void onPreExecute() {
ProgressDialog myDialog = new ProgressDialog(context);
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myDialog.setMax(maxValue);
myDialog.show();
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加myDialog.show();
有几种方法可以通过增加金额或将进度设置为特定值来更新进度:
@Override
protected void onProgressUpdate(Integer... progress) {
myDialog.incrementProgressBy(progress[0]);
// or
// myDialog.setProgress(progress[0]);
}
Run Code Online (Sandbox Code Playgroud)
然后在onDoInBackground()中:
@Override
protected void doInBackGround() {
// your code
publishProgress(1);
}
Run Code Online (Sandbox Code Playgroud)
布局中带有进度条的编辑示例:
在布局文件中,添加这样的进度条
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>
Run Code Online (Sandbox Code Playgroud)
在你的asynctask:
protected void onPreExecute() {
ProgressBar myProgress = findViewById(R.id.progressbar);
myProgress.setMax(maxValue);
myProgress.setVisibility(View.VISIBLE);
}
protected void onProgressUpdate(Integer... progress) {
myProgress.setProgress(progress[0]);
}
Run Code Online (Sandbox Code Playgroud)