ProgressDialog未显示

时间:2015-08-15 19:15:27

标签: java android

我正在尝试在后台处理一些数据时显示ProgressDialog。

我在启动Thread之前调用方法show(),它没有显示,但是当我在Thread中调用方法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();
    }
}

你怎么看我用几乎网络刷新一个List。

1 个答案:

答案 0 :(得分:0)

试试这个:

progress = ProgressDialog.show(this, "dialog title",
    "dialog message", true);

请参阅文档 - 静态show(...)方法。 http://developer.android.com/reference/android/app/ProgressDialog.html