2014-04-01 141 views
0

如何檢查手機的分組數據是否連接。因爲當我在我的android應用上啓用移動數據選項時,即使在我的SIM卡中沒有任何負載,它也會始終連接。我的問題是如何驗證是否有互聯網連接或不在我的Android應用程序。如何檢查移動網絡是否連接到互聯網或不在android

btw這裏是我的代碼。

ConnectivityManager cm = (ConnectivityManager)this.GetSystemService(Context.ConnectivityService); 



    NetworkInfo nf = cm.ActiveNetworkInfo; 
    if (nf != null && nf.IsConnected == true) 
    { 
//connected 
    } 
    else 
    { 
//not connected 
    } 

但是在這段代碼中。即使我失去了負荷。我的應用程序顯示我仍然連接。幫助請..

+0

爲什麼你添加了C#標籤? – Kedarnath

+0

在這種情況下,「連接」並不意味着它正在主動發送或接收數據 - 它只是意味着有可能發送/接收數據。 – Squonk

+0

連接到什麼?如果您的手機連接到Wifi路由器,可能仍然沒有互聯網連接。如果Wifi路由器連接到ISP,則可能仍然沒有前向連接。如果您想知道您是否可以訪問特定網站,請發送請求。 –

回答

1

試試這個:

boolean internetCheck; 
internetCheck = isInternetAvailable(this); 
if (internetCheck) { 
       //Internet available 
      } else { 
       //No Internet available   } 
    /** 
     * 
     * Method to check internet connection is available 
     */ 

     public static boolean isInternetAvailable(Context context) { 
      boolean haveConnectedWifi = false; 
      boolean haveConnectedMobile = false; 
      boolean connectionavailable = false; 

      ConnectivityManager cm = (ConnectivityManager) context 
        .getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 

      NetworkInfo informationabtnet = cm.getActiveNetworkInfo(); 
      for (NetworkInfo ni : netInfo) { 
       try { 

        if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
         if (ni.isConnected()) 
          haveConnectedWifi = true; 
        if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
         if (ni.isConnected()) 
          haveConnectedMobile = true; 
        if (informationabtnet.isAvailable() 
          && informationabtnet.isConnected()) 
         connectionavailable = true; 

       } catch (Exception e) { 
        // TODO: handle exception 
        System.out.println("Inside utils catch clause , exception is" 
          + e.toString()); 
        e.printStackTrace(); 

       } 
      } 
      return haveConnectedWifi || haveConnectedMobile; 
     } 
0

試試這個

//call web service 
    ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE); 
    boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting(); 
    boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting(); 
    //check Internet connection 
    if(internet||wifi) 
    { 
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 
    }else{ 
        Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show(); 
      }  

同時添加下列權限到您的清單文件

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
0
Try this one.   
     ConnectivityManager connectivityManager = (ConnectivityManager) context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager 
       .getActiveNetworkInfo(); 
     boolean isActive=activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
       if(isActive) 
       { 
       System.out.print("Connection Establied"); 
        } 
       else{ 
        System.out.print("Connection not Establied"); 
        } 
0

試試這個

public class ConnectionDetector { 
private Context _context; 

public ConnectionDetector(Context context){ 
    this._context = context; 
} 

public boolean isConnectingToInternet(){ 
    ConnectivityManager connectivity = (ConnectivityManager) _context.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; 
} 
} 

而且在活動文件

 ConnectionDetector cd= new ConnectionDetector(context); 

    boolean isInternetPresent = cd.isConnectingToInternet(); 

如果互聯網出現時,它會返回true