可能重复:
how to check wifi or 3g network is available on android device
Check the Internet Connectivity in Android
我正在使用Android设备,我正在禁用设备中的WiFi连接。现在我有一个使用Internet的应用程序,每当应用程序运行时,我想显示一条简单的消息,即Toast消息告诉“没有Internet连接“而不是应用程序的崩溃。
怎么做?
答案 0 :(得分:1)
public final boolean isInternetOn()
{
ConnectivityManager connec = (ConnectivityManager) getSystemService
(Context.CONNECTIVITY_SERVICE);
if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
{
return true;
}
else
return false;
}
答案 1 :(得分:0)
尝试使用此代码段:
ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE);
if( ! ( cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) )
{
android.util.Log.e(LOGTAG, "No connectivity to send information...");
return;
}