简体   繁体   中英

ProgressDialog not showing

I'm trying to show a ProgressDialog while I'm processing some data on background.

I call the method show() before starting the Thread, and it doesn't show, but when i call inside the Thread the method dismiss(), it appears and desapears in a flash.

I read some about using an Async Task, but I really don't want to show a progress, just the spinning that ads the user that the app is loading.

How can I solved this?

Some of my code:

// 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();
}

I declared the progress var like this:

private ProgressDialog progress;
public void onCreate(Bundle savedInstanceState) {
    //[...]
    progress  = new ProgressDialog(this);
    //[...]
}

The Scanner class code:

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();
    }
}

How you can see I'm refreshing a List with nearly networks.

Try this:

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

See documentation - static show(...) methods. http://developer.android.com/reference/android/app/ProgressDialog.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM