java - 进度对话框未显示

标签 java android

我正在尝试在后台处理一些数据时显示 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/

相关文章:

java - 连接到远程主机时 VisualVM 的问题

java - 对象序列化/反序列化和 FileInputStream.available()

java - 通过减法将查询替换为连字符

android - 要求用户观看插页式广告?

java - SQLite 如果存在现有数据则跳过或覆盖

java - 错误: GC overhead limit exceeded in XSSFWorkbook

java - EditText.getText().toString() 崩溃

安卓:自适应阈值

android - 您可以使用 Google Places API 搜索与特定企业列表相匹配的附近企业吗?

android - 如何在Google Glass上安装和启动Metaio应用?