2016-11-23 33 views
0

我的應用程序需要在有免費公共WiFi網絡的工作場所/機場工作。如何檢測用戶何時連接到公共WiFi但未獲批准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("************************************************************************************"); 

} 

我得到了相同的結果時,我之前並點擊網頁瀏覽器後進行檢查。 請指教,謝謝 , 路德

+0

您可以嘗試「ping」此服務器,如果它現在連接,它將無法工作。 –

回答

0

你應該做一個連通性檢查,以驗證您是捕獲門戶圍牆花園內啓動。

對此here on 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(); 
    } 

的官方文檔是慷慨地提供了一系列examples,其中包括處理網絡登錄,HTTP認證爲以及處理cookie。你應該看看它。 希望這有助於!

+0

@koko您是否設法使其工作? – HenriqueMS