简体   繁体   中英

Android: Pause / Wait activity after enabling service

I've got a very simple app which lists the available WiFi access points (routers).

I have an AlertDialog set up whenever the app opens or resumes & WiFi isn't enabled. This takes a user to the WiFi settings where they turn on WiFi.

The user then hits the back button and returns to my app. My app then sees that WiFi is enabled and outputs a list.

The problem is, even though WiFi is enabled - the AP list isn't yet done (even though I'm checking if the scan has finished.

So, how can I pause my activity long enough so that I get a complete list?

WifiManager oWiFiManager = (WifiManager) getSystemService(WIFI_SERVICE);

// Only proceed if WiFi is Enabled
if (oWiFiManager.isWifiEnabled())
{
    boolean bScanComplete = oWiFiManager.startScan();

    if (bScanComplete)
    {
        /* Scan is complete. Safe to proceed.
         * This is the problem area because for some reason there are no networks listed (but there are in reality).
         */
    }
}

else
{
    // Tell user WiFi is Disabled & take back to settings dialog
}

You should be able to use the ConnectivityManager to get the state of the Wifi adapter. From there you can check if it is connected or even available.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
 // Do whatever
}

      Dont forget to add android.permission.ACCESS_NETWORK_STATE to your AndroidManifest.xml for this to work

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