我正在使用一个代码检查用户是否已激活互联网,但是在将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;
}
Run Code Online (Sandbox Code Playgroud) android ×1