2013-11-02 30 views
1

更新:基本上我需要確認我的網絡是私人網絡還是與外部世界連接,即互聯網。例如,我在WiFi網絡上,但路由器互聯網線路未連接。雖然網絡連接,但互聯網不可用。Android應用程序確認Internet工具是否可用?

需要展現在我的Android應用程序網絡的詳細信息,

  • (一)是否N/W是否可用
  • (B)高速上網服務是否可用

目前,我認爲www.google.com總是會啓動,並且低於代碼才能正常工作。但是這個解決方案很慢,並且基於www.google.com總是會啓動的假設。

有沒有更好的選擇來確認互聯網設施是否可用在網絡上?

public static boolean isConnected(Context context){ 
     NetworkInfo info = Connectivity.getNetworkInfo(context); 
     return (info != null && info.isConnected()); 
    } 

public boolean checkInternetConnectivity() { 

     try { 
      HttpURLConnection urlc = (HttpURLConnection) (new URL(
        "http://www.google.com").openConnection()); 
      urlc.setRequestProperty("User-Agent", "Test"); 
      urlc.setRequestProperty("Connection", "close"); 
      urlc.setConnectTimeout(3000); 
      urlc.setReadTimeout(4000); 
      urlc.connect(); 
      isInterNetAvailable = true; 
      return (urlc.getResponseCode() == 200); 
     } catch (IOException e) { 
      isInterNetAvailable = false; 
      return (false); 
     } 
    } 
public String getNWConnectivityText() { 
    if (isConnected(getSherlockActivity()) == true) { 

     if (true == checkInternetConnectivity()) { 
      return ""N/W is connected and Internet is working!"; 
     } else { 
      return "N/W is connected but Internet is NOT working!"; 
     } 

    } else { 
     return "N/W is not connected!"; 
    } 
} 

回答

0

使用此方法來檢查互聯網連接從您的活動

public boolean isInternetConnected() { 
     ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = cManager.getActiveNetworkInfo(); 

     if(networkInfo == null) 
      return false; 
     else if(!networkInfo.isConnected()) 
      return false; 
     else if(!networkInfo.isAvailable()) 
      return false; 
     else 
      return true; 
    } 

此外,添加這些權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+0

更新:基本上我需要確認我的網絡是專用網絡或與外界相通,即互聯網。例如,我在WiFi網絡上,但路由器互聯網線路未連接。雖然網絡連接,但互聯網不可用。那麼你的解決方案會工作嗎 – RDX

0

其實我一直使用這個ConnectionController類。它檢查兩個WiFi移動連接

public class ConnectionController { 

    /** NEEDS PERMISSIONS NETWORK AND WIFI STATE */ 

    /** 
    * checks if a mobile or wifi connection is available or about to be available 
    * sends a proper response for user information 
    * @param context 
    * @return 
    */ 
    public static boolean isConnected(Context context) { 

     if(context != null) { 

      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo netinfo = cm.getActiveNetworkInfo(); 

      if (netinfo != null && netinfo.isConnectedOrConnecting()) { 
       android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
       android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

       if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true; 
       else return false; 
      } else 
       return false; 
     } else return false; 
    } 
} 

在代碼中,你可以檢查您的網絡連接是這樣的:

if(ConnectionController.isConnected(Context)) { 
    // do something if connection available 
} 
+0

更新:基本上我需要確認我的網絡是私人網絡還是與外部世界連接,即互聯網。例如,我在WiFi網絡上,但路由器互聯網線路未連接。雖然網絡連接,但互聯網不可用。 – RDX

0

您可以使用此方法來檢查網絡連接

public boolean isConnectingToInternet(){ 
     ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
      if (connectivity != null) 
      { 
       NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
       if (info != null) 
        for (int i = 0; i < info.length; i++) 
         if (info[i].getState() == NetworkInfo.State.CONNECTED) 
         { 
          return true; 
         } 

      } 
      return false; 
    } 

也包括按照清單文件權限:

<uses-permission android:name="android.permission.INTERNET" /> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

我希望這可以幫助你。

+0

更新:基本上我需要確認我的網絡是私人網絡還是與外部世界連接,即互聯網。例如,我在WiFi網絡上,但路由器互聯網線路未連接。雖然網絡連接,但互聯網不可用。那麼你的解決方案會工作嗎 – RDX

+1

在這種情況下,您可以使用connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)而不是connectivity.getAllNetworkInfo(),並將info.isconnected()的結果存儲在布爾變量中。檢查結果,你會得到答案。 –

1
public static boolean isConnected(Context context) { 
    final ConnectivityManager conMgr = (ConnectivityManager) context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); 
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) { 
     return true; 
    } else { 
     return false; 
    } 
} 

添加到清單文件:

<uses-permission android:name="android.permission.INTERNET" /> 
+0

更新:基本上我需要確認我的網絡是一個專用網絡還是與外部世界連接,即互聯網。例如,我在WiFi網絡上,但路由器互聯網線路未連接。雖然網絡連接,但互聯網不可用。那麼你的解決方案會工作嗎 – RDX

相關問題