Android中如何判断数据连接是否可用

2020-08-30  本文已影响0人  XiaoXred

public static boolean isNetworkAvailable(Context context) {

try {

ConnectivityManager manager = (ConnectivityManager) NetworkUtils.getSystemService(context,

                Context.CONNECTIVITY_SERVICE);

        if (manager !=null) {

NetworkInfo[] networkInfos = manager.getAllNetworkInfo();

            if (networkInfos !=null) {

for (NetworkInfo networkInfo : networkInfos) {

if (networkInfo !=null && networkInfo.isConnected()) {

return true;

                    }

}

}

}

}catch (Exception e) {

}

return false;

}

上一篇下一篇

猜你喜欢

热点阅读