2011-04-18 40 views

回答

4

嘿,夥計.. 。申請此代碼..這可能有助於提前u.thanks ....

布爾連接;

private boolean checkInternetConnection() 
    { 

     ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); 

     // ARE WE CONNECTED TO THE NET 

     if (conMgr.getActiveNetworkInfo() != null 

       && conMgr.getActiveNetworkInfo().isAvailable() 

       && conMgr.getActiveNetworkInfo().isConnected()) 
     { 

     return true; 

     } 
     else 
     { 
     return false; 

     } 
    } 
2
 ConnectivityManager connectionService = 
      (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
     if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) { 
      // Device is online 
     } 
1

使用下列函數序上網查connction:

public boolean isOnline() 
    { 

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     boolean result = false; 
     if(ni != null) 
     { 
      if( ni.getState() == NetworkInfo.State.CONNECTED) 
      { 
       result = true; 
      } 
     } 

     return result; 

     } 

檢查什麼isOnline()回報。如果真的,那麼互聯網連接其他Inernet沒有連接。 希望這會幫助你.. :)

0

使用這個答案它的幫助。

public boolean isOnline() { 
    ConnectivityManager cm =(ConnectivityManager)  
    getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
    return true; 
    } 
    return false; 
} 

if(temp==true){ 
    genHelper.showToast("Net Connection");    
}else{    
    genHelper.showToast(" Not Connected"); 
} 
1
// Network is connected or not 
public boolean isConnected() 
{ 
    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) 
        { 
         Log.d("LOG","Network is Available"); 
         return true; 
        } 

     } 
     return false; 
} 
相關問題