检查网络连接问题
我有一个 IntentService 可以进行一些 Web 服务调用。在拨打这些电话之前,我会检查以确保设备具有网络连接。我这样做是这样的:
private boolean isOnline() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
return connec.getNetworkInfo(0).isConnectedOrConnecting();
}
不幸的是,当我在 Android 设备上进行调试时,当我同时拥有网络和无线连接时,这会返回 false。
关于 connec.getNetworkInfo(0) 的一些有趣的花絮:
mIsAvailable = true
mNetworkType = 0
mTypeName = "mobile"
mState.name = "DISCONNECTED"
显然这段代码是不够的(也许只有当我通过网络发送一些位并打开无线电时它才会返回 true?)。此外,由于我不太熟悉 ConnectivityManager,我假设我应该扫描所有网络(即:getNetworkInfo(0 到 N))。
我怎样才能在这里正确地完成我想要的事情?
I have an IntentService which makes some web service calls. Before making these calls I check to make sure the device has network connectivity. I am doing so like this:
private boolean isOnline() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
return connec.getNetworkInfo(0).isConnectedOrConnecting();
}
Unfortunately, when I'm debugging on my Android device, this returns false when I have both a network and a wireless connection.
Some interesting tidbits about connec.getNetworkInfo(0):
mIsAvailable = true
mNetworkType = 0
mTypeName = "mobile"
mState.name = "DISCONNECTED"
Clearly this code is not sufficient (perhaps it would only return true if I sent some bit over the network and turned the radio on?). Moreover, since I'm not well versed in the ConnectivityManager, I'm assuming I should probably be scanning all networks (ie: getNetworkInfo(0 through N)).
How can I properly accomplish what I'm wanting here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: