在无网或网速差的状态下,没必要去连接服务器。
你可以使用 ConnectivityManager 来判断是否连到网络,以及网络类型。
判断是否有网络连接
下面的代码用ConnectivityManager查询是活动网络连接判断是否有Internet连接。
代码如下:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo()