【发布时间】:2012-04-27 12:16:13
【问题描述】:
我正在做一个应用程序,它在整个应用程序中使用 Internet 连接。如果在使用应用程序时互联网连接丢失,应用程序将被强制关闭。为避免这种情况,如果互联网不可用,我想显示一条警报消息。我怎样才能做到这一点。 在登录时,我正在使用以下代码检查连接。但是我怎样才能在后台为整个应用程序做到这一点。
private boolean haveInternet(){
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info==null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to disable internet while roaming, just return false
return true;
}
return true;
}
谢谢你..
【问题讨论】:
-
@parag 你能举个例子吗?
标签: android