我正在尝试在后台处理一些数据时显示 ProgressDialog。
我在启动线程之前调用show()方法,它没有显示,但是当我在线程内部调用方法dismiss()时,它会出现并立即消失。
我阅读了一些有关使用异步任务的内容,但我真的不想显示进度,只是向用户展示应用程序正在加载的旋转。
我该如何解决这个问题?
我的一些代码:
// When clicking a button a call this method to start the thread
public void onClick(View v) {
// Here, doesn't show the spinning wheel
progress = ProgressDialog.show(this,
"Wait please …",
"Scanning …",
true);
Thread scan = new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Scanner());
progress.dismiss();
}
});
scan.start();
}
我声明了 progress
var,如下所示:
private ProgressDialog progress;
public void onCreate(Bundle savedInstanceState) {
//[...]
progress = new ProgressDialog(this);
//[...]
}
Scanner
类代码:
private class Scanner implements Runnable {
private final String TAG = "SCANNER-->";
public void run() {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
for (int i = 0; i < 10; i++) {
wifiManager.startScan();
List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
final int size = results.size();
if (size == 0) {
adapter.clear();
adapter.add("No access points in range");
break;
}
else {
txt.setText("Number of results: " + results.size());
Log.d(TAG,"Number of results: " + results.size());
for (ScanResult result : results) {
if (adapter.getPosition(result.SSID) == -1) {
adapter.add(result.SSID);
}
}
}
}
else {
adapter.clear();
adapter.add("No results. Check wireless is on");
break;
}
adapter.notifyDataSetChanged();
Log.d(TAG,"sistema avisado de cambios");
// Refresh information each 0.5 second
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progress.dismiss();
}
}
您如何看到我正在刷新包含近网络的列表。
最佳答案
试试这个:
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
请参阅文档 - 静态 show(...)
方法。
http://developer.android.com/reference/android/app/ProgressDialog.html
关于java - 进度对话框未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32028378/