我正在做后台处理。同时,我想向用户显示圆形进度栏。后台进程完成后,我想隐藏圆形进度条。但是,当我在showDialog()之后运行somethign时,它消失了。后台进程正在进行时如何显示此对话框?
MainActivity.java
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int pStatus = 0;
Handler handler = new Handler();
public static Dialog circularProgressBarDialog;
public static Thread circularProgressBarThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
circularProgressBarDialog = new Dialog(this);
circularProgressBarDialog.setContentView(R.layout.dialog_circular_progressbar);
progressBar = circularProgressBarDialog.findViewById(R.id.progressBar);
circularProgressBarDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
circularProgressBarDialog.setCancelable(false);
if(circularProgressBarThread != null && circularProgressBarThread.isAlive()){
circularProgressBarThread.interrupt();
}
circularProgressBarThread = new Thread(new Runnable() {
@Override
public void run() {
while (pStatus <=100) {
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(pStatus);
Log.d("tarik","pStatus : " + pStatus);
if(pStatus++ >= 100){
pStatus = 0;
}
}
});
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
circularProgressBarThread.start();
new Handler().post(new Runnable() {
@Override
public void run() {
circularProgressBarDialog.show(); // dialog appears
/* dialog disappears when this for loop runs!! WHY !!!! */
for (int i = 0; i < 200000; i++) {
Log.d("heavy work","1");
}
circularProgressBarDialog.dismiss();
circularProgressBarThread.interrupt();
}
});
}
}
custom_progressbar_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270" >
<shape
android:shape="ring"
android:useLevel="false" >
<gradient
android:centerY="0.5"
android:endColor="#FA5858"
android:startColor="#0099CC"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
dialog_circular_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/custom_progressbar_drawable"
android:secondaryProgress="0" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:1)
尝试对进度对话框使用setCancelabl(false)。 您也可以为此目的使用异步任务。
答案 1 :(得分:0)
尝试更换
for (int i = 0; i < 200000; i++) {
Log.d("heavy work","1");
}
做一些繁重的工作。从0到200000的循环对我来说似乎并不繁重。尝试:
Thread.sleep(5000)
或增加一些延迟以表示繁重的工作。请记住,实际进度条应使用实际进度进行更新,或者将其不确定地设置为true。您可以通过在“工作”块内设置进度来完成进度的真实表示。如果您在UI线程外更新视图,请记住调用runOnUiThread()。