0
我已经研究了大部分问题,但没有找到可行的解决方案。如何在Android中处理互联网连接问题
我正在设计一个高度使用互联网连接的应用程序。我永久需要从Web服务请求/接收信息。
我试过sourounding使用互联网的所有代码
try
{
//use of internet here
}
catch (Exception e)
{
//Alert dialog that states internet connection is down
}
但这似乎并没有工作。由于某种原因,这里没有捕捉到这个例外。
我试过解决方法,我在这里找到的解决方案。用途:
public static boolean isOnline(Context context) {
ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
,并用它喜欢:
if (Utilities.IsOnline(context))
{
//Code that uses internet
}
如果连接丢失后右IsOnline检查,会发生什么? 我该如何治疗这种情况? 谢谢!
请考虑您的网络连接如何影响应用程序的电池寿命。如果您正在大量使用网络,值得一看ARO工具,它会告诉您是否正确缓存数据,这可能会对应用程序的性能产生很大影响http://developer.att.com/ARO –