简体   繁体   English

Android:启用服务后暂停/等待活动

[英]Android: Pause / Wait activity after enabling service

I've got a very simple app which lists the available WiFi access points (routers). 我有一个非常简单的应用程序,其中列出了可用的WiFi接入点(路由器)。

I have an AlertDialog set up whenever the app opens or resumes & WiFi isn't enabled. 每当应用打开或恢复且未启用WiFi时,我都会设置一个AlertDialog。 This takes a user to the WiFi settings where they turn on WiFi. 这会将用户带到他们打开WiFi的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. 然后,我的应用程序看到已启用WiFi并输出列表。

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. 问题是,即使启用了WiFi,AP列表仍未完成(即使我正在检查扫描是否已完成)。

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. 您应该能够使用ConnectivityManager来获取Wifi适配器的状态。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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