在开发中,我们在进行网络读取时,常常需要先判断手机是否联网,如何判断联网呢,请参阅下面关键代码:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetHelper {
//是否联网网络
public static boolean IsHaveInternet(final Context context) {
try {
ConnectivityManager manger = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manger.getActiveNetworkInfo();
return (info!=null && info.isConnected());
} catch (Exception e) {
return false;
}
}
}