【发布时间】:2019-07-30 18:15:07
【问题描述】:
我正在使用一个代码来检查用户是否有互联网活动,但是在针对 sdk29 之后,下面的功能被弃用了
网络信息
NetworkInfo.isConnected()
getActiveNetworkInfo()
代码如下:
public static boolean isNetworkAvailable(Context context) {
if(context == null) { return false; }
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// if no network is available networkInfo will be null, otherwise check if we are connected
try {
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
Log.i("update_statut","Network is available : true");
return true;
}
} catch (Exception e) {
Log.i("update_statut",""+ e.getMessage());
}
Log.i("update_statut","Network is available : FALSE ");
return false;
}
【问题讨论】:
标签: android