检查 Android 上的互联网连接
我有以下代码用于检查我的应用程序上的互联网连接 wifi/EDGE/GPRS/3G。
代码是
public static boolean checkConn(Context ctx) {
ConnectivityManager conMgr = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
} else if (conMgr.getNetworkInfo(0).getState()==NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED){
return false;
}
return false;
}
,我这样称呼它:
if (CheckInternet.checkConn(introPage.this) == true) {
Intent toMainPage = new Intent(introPage.this, mainPage.class);
System.gc();
startActivity(toMainPage);
} else if (CheckInternet.checkConn(getApplicationContext()) == false) {
Toast.makeText(getApplicationContext(),
"Sorry, No internet connectivity found", Toast.LENGTH_SHORT)
.show();
}
但是我遇到了一个问题,那就是如果我连接到wifi,并且我打开应用程序,它工作正常,但是如果我关闭应用程序并关闭wifi并重新-打开应用程序,它不会出现“无连接”错误,我需要关闭设备然后再打开它,同样的情况是如果 wifi 关闭,并且我打开应用程序,它会抛出“无连接”错误”,如果我打开它,它仍然会抛出相同的“无连接”错误,除非我关闭并打开设备。
I have the following code for checking internet connection wifi/EDGE/GPRS/3G on my application.
the code is
public static boolean checkConn(Context ctx) {
ConnectivityManager conMgr = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
} else if (conMgr.getNetworkInfo(0).getState()==NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED){
return false;
}
return false;
}
and I am calling it like below :
if (CheckInternet.checkConn(introPage.this) == true) {
Intent toMainPage = new Intent(introPage.this, mainPage.class);
System.gc();
startActivity(toMainPage);
} else if (CheckInternet.checkConn(getApplicationContext()) == false) {
Toast.makeText(getApplicationContext(),
"Sorry, No internet connectivity found", Toast.LENGTH_SHORT)
.show();
}
But I am having an issue, which is that if I am connected to wifi, and I open the application, it works fine, but if I close application and turn off wifi and re-open application, it doesn't through the error of "no connection" , I need to turn off my device and then turn it on, and same case is if wifi is off, and I open application, it throws error of "no connection", and if I turn it on, still it throws the same error of "no connection", until unless I turn off and on device.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有时活动连接不是列表中的第一个连接,或者处于非活动状态或处于错误状态。我将这样做:
[编辑1]不要忘记在应用程序清单中添加此权限:
这对您有帮助吗?
以马内利
Sometimes the active connection is not first in the list, or is inactive or in an error state. This is how I would do it:
[EDIT 1] Don't forget to add this permission in the application manifest:
Does this help you?
Emmanuel
简短的回答:
The short answer:
更好:
使用另一种形式,如果“Network i”等于 null,则检查
!i.isConnected()
必须失败(i 为 null)。Is better:
with the other form, if "Network i" is equal null then, check after for
!i.isConnected()
must fail (i is null).无线上网-
wifi-
试试这个:
这个权限:
Try this:
and this permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
您好,尝试以下代码:
在 Android Manifest 文件中添加以下 3 个权限。
Hi try the following code :
Add below 3 permissions in Android Manifest File.
我曾经检查是否有连接,不要忘记检查 NetworkInfo 是否为 null,因为在不提供移动数据连接的平板电脑上,TYPE_MOBILE 的 NetworkInfo 返回 null。
I used to check if I have connectivity, don't forget to check if the NetworkInfo is null or not because on tablet where mobile data connectivity is not provided, the NetworkInfo for TYPE_MOBILE return null.
与批准的答案相同,但简而言之:
same as the approved answer but in short :
您可以使用这个很棒的 emil2k 的要点:
You can use this awesome gist by emil2k :