2011-03-28 47 views
4

我真正想要的是,當用戶沒有連接時,我想顯示一些他或她沒有聯繫的對話框。如何在Android上檢查我的互聯網訪問?

我試圖把這個放在我的MainActivity上,但仍然沒有工作。

public boolean isOnline() { 
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 

} 

我也用這一個,但它也沒工作:

public class ConnectionChangeReceiver extends BroadcastReceiver 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
     NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE); 
     if (activeNetInfo != null) 
     { 
      Toast.makeText(context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
     } 
     if(mobNetInfo != null) 
     { 
      Toast.makeText(context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
     } 
     } 
    } 

我說這對我的清單:

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

誰能告訴我我在做什麼錯了,我真的很感激它。提前致謝!

回答

2

也許這個鏈接可以幫助你遠一點:Broadcastreceiver to obtain ServiceState information

,你可以過去在您的活動中精心設計的CommunicationManager代碼,或者更好,如果您需要更多活動的訪問權限,您還可以在應用程序Si中聲明該類爲靜態ngletin實例和描述here

從那裏調用它,你也需要在你的應用程序清單註冊的BroadcastReceiver:

<receiver android:enabled="true" android:name="com.project.mobile.controller.comm.ConnectivityReceiver"> 
    <intent-filter> 
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
    </intent-filter> 
</receiver> 

享受

2
public static boolean haveInternet(Context ctx) { 
     NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 
     if (info == null || !info.isConnected()) { 
      return false; 
     } 
     return true; 
    } 

如果布爾方法返回true,則有互聯網連接,如果假無連接..

if(haveInternet==true){ 
    //Some code 
}else{ 
    Toast.makeText(ActivityName.this,"No Internet Connection",  Toast.LENGTH_SHORT).show(); 
} 

也看到這進一步幫助.. Internet Connection ...

+0

我應該把這個放在我的主要活動? – 2011-03-28 13:13:36

+0

是的,你可以把它放在你的活動中。但對流的方式是使用像internetUtil這樣的全球類之一併在代碼中的任何地方使用後粘貼 – milind 2011-03-28 13:20:22

+0

是的,你可以把它放在你的活動中。你明白了嗎? – Venky 2011-03-28 14:35:12

0

u能檢查在ConnectivityManager類的幫助下,您的內部連接。 我已經給出了一個你可以在代碼中使用的函數。

public static boolean 
CheckInternet(Context context)  { 
     ConnectivityManager connec =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 

     android.net.NetworkInfo wifi = > connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

     android.net.NetworkInfo mobile =connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

這裏如果WiFi和移動網絡條件檢查可用 與否。如果他們中的任何一個可用或連接,則將 返回true,否則爲false;

if (wifi.isConnected()) {   
return true;   
} else if 
(!mobile.isConnected()) 
{   
return false;  
} else if 
(mobile.isConnected()) 
{   
return true;   
} return false; 
} 

使用此功能後u需要2只檢查情況類似

if (CheckInternet(getBaseContext())) 
{ 
//connection successfull 

} else 

{ Toast.makeText(getBaseContext(),"Please Check InternetConnectionToast.LENGTH_LONG).show(); 
} 

享受

+0

我應該在我的活動中過去嗎? – 2011-03-28 13:16:57

+0

是的,你可以粘貼你的活動......但是如果你在所有的活動中都需要它,那麼你必須爲此創建一個類,然後粘貼這個函數......或者創建一個類並使上面的函數靜態...所以你可以使用類名來調用它,你只需要傳遞當前活動的上下文.... – 2011-03-28 13:18:16

+0

@chirag:似乎@milind複製/粘貼你的迴應:) http://stackoverflow.com/questions/5373930/how-to-check-network-connection-enable-or-disable-in-wifi-and-3gdata -plan-in-mo/5374040#5374040 – tbruyelle 2011-03-28 15:49:33

1

只要使用這種方法

private boolean isNetworkConnected(Context context) { 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo network = cm.getActiveNetworkInfo(); 
     if (network != null) { 
      return network.isAvailable(); 
     } 
     return false; 
    } 
0

的當您連接到Wi-Fi資源或通過手機數據包時,上述方法可以工作。但是在Wi-Fi連接的情況下,有些情況下您還需要像咖啡廳一樣登錄。所以在這種情況下,您的應用程序會因爲您連接到Wi-Fi源而不是Internet而失敗。

此方法正常工作。因爲它使網絡電話,將拋出NetwrokOnMainThreadException如果不按照一個單獨的線程

public static boolean isConnected(Context context) { 
    ConnectivityManager cm = (ConnectivityManager)context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
    if (activeNetwork != null && activeNetwork.isConnected()) { 
     try { 
      URL url = new URL("http://www.google.com/"); 
      HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); 
      urlc.setRequestProperty("User-Agent", "test"); 
      urlc.setRequestProperty("Connection", "close"); 
      urlc.setConnectTimeout(1000); // mTimeout is in seconds 
      urlc.connect(); 
      if (urlc.getResponseCode() == 200) { 
       return true; 
      } else { 
       return false; 
      } 
     } catch (IOException e) { 
      Log.i("warning", "Error checking internet connection", e); 
      return false; 
     } 
    } 

    return false; 

} 

請使用主線程。

相關問題