如何确定在Android上的网络不可用 [英] How to determine the network unavailability in android
问题描述
我要显示警报该连接不可用在我的Android应用程序。
I want to show alert that connection not available in my android application.
我打电话在我的应用一些休息的请求。要检查这个网络不可用情况下我手动断开我的无线我的笔记本电脑(我在模拟器测试)。
I am calling some rest request in my application. To check this network unavailability scenario I manually disconnect my wifi in my laptop (I am testing in simulator) .
code,其中我打电话的服务是这里
Code where I call service is here
resp = client.execute(httpPostRequest);
和我在这里捕获的异常
catch (IllegalStateException e) {
//assertTrue(e.getLocalizedMessage(), false);
e.printStackTrace();
} catch (ClientProtocolException e) {
//assertTrue(e.getLocalizedMessage(), false);
e.printStackTrace();
} catch (IOException e) {
//assertTrue(e.getLocalizedMessage(), false);
e.printStackTrace();
}
和当我例外,我试图让与设备的网络连接的状态
此功能
and when I get exception I try to get the status of the device network connection with this function
public boolean IsInternetConnectted()
{
ConnectivityManager conMgr = (ConnectivityManager)this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
conMgr = null;
if (i == null)
return false;
if (!i.isConnected())
return false;
if (!i.isAvailable())
return false;
return true;
}
即使无线网络连接断开,从这个功能我模拟器返回true。
even if wi-fi is disconnected my simulator return true from this function.
如何解决这个situaltion ??
How to resolve this situaltion??
推荐答案
我在模拟器研究发现,同样的错误......
但尝试在移动设备上的code此code完美运行而不是现在在实际设备中的任何错误...
I Found that same error in my Emulator... But Try your Code on Mobile Device this code Run Perfectly and not coming any error in real Device...
这篇关于如何确定在Android上的网络不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!