0

我的应用程序需要在有免费公共 wifi 网络的工作场所/机场上运行。

当他需要通过网络浏览器批准 wifi 使用条款时,我想通知用户。

我试图提取信息,但没有找到任何属性/信息来隔离这种情况

public static void haveNetworkConnection(Context mContext) {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();


    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi
            Toast.makeText(mContext, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            printWifiInformation(activeNetwork);
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            Toast.makeText(mContext, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(mContext, "Not connected", Toast.LENGTH_SHORT).show();
    }
}

public static void printWifiInformation(NetworkInfo activeNetwork){

    System.out.println("************************************************************************************");
    System.out.println("1 . describeContents         " + activeNetwork.describeContents());
    System.out.println("2 . getDetailedState         " + activeNetwork.getDetailedState());
    System.out.println("3 . getExtraInfo             " + activeNetwork.getExtraInfo());
    System.out.println("4 . getReason                " + activeNetwork.getReason());
    System.out.println("5 . getState                 " + activeNetwork.getState());
    System.out.println("6 . getSubtype               " + activeNetwork.getSubtype());
    System.out.println("7 . getSubtypeName           " + activeNetwork.getSubtypeName());
    System.out.println("8 . getSubtypeName           " + activeNetwork.getSubtypeName());
    System.out.println("9 . getType                  " + activeNetwork.getType());
    System.out.println("10. getTypeName              " + activeNetwork.getTypeName());
    System.out.println("11. isAvailable              " + activeNetwork.isAvailable());
    System.out.println("12. isConnected              " + activeNetwork.isConnected());
    System.out.println("13. isConnectedOrConnecting  " + activeNetwork.isConnectedOrConnecting());
    System.out.println("14. isFailover               " + activeNetwork.isFailover());
    System.out.println("15. isRoaming                " + activeNetwork.isRoaming());
    System.out.println("************************************************************************************");

}

在单击 Web 浏览器之前和之后检查时,我得到了相同的结果。请指教,谢谢,路德

4

1 回答 1

0

您应该首先进行连接检查,以验证您是否位于Captive Portal 围墙花园内。

在 SO 上有一个很好的解释。

尽管如此,这并没有为您提供有关强制门户本身的信息。

因此,您将不得不手动处理HttpUrlConnection响应标头并寻找重定向

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     if (!url.getHost().equals(urlConnection.getURL().getHost())) {

      // we were redirected! Kick the user out to the browser to sign on?
     }
     ...
   } finally {
     urlConnection.disconnect();
   }

官方文档足够丰富,提供了一系列示例,包括处理网络登录、Http 身份验证以及处理 cookie。你应该看看。希望这可以帮助!

于 2016-11-23T17:14:14.913 回答