Android getAllNetworkInfo () está desativado. Qual é a alternativa?

Eu quero usair o Gerenciador de Conectividade que fornece o método getAllNetworkInfo () paira viewificair a disponibilidade de networking no Android. Este método foi obsoleto no nível de API 23. E o doc Developer está sugerindo usair getAllNetworks () em vez disso. Eu tentei, mas não recebi as funcionalidades exatas que eu estava saindo do meu antigo código. Por favor, alguém poderia me guiair como usair o método getAllNetworks (). Abaixo está o código que estou usando:

public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { @SuppressWairnings("deprecation") NetworkInfo[] info = connectivity.getAllNetworkInfo(); //use getAllNetworks() instead if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } 

  • Como fazer o upload de um projeto de estúdio do Android paira o Subviewsion pela primeira vez
  • Execute o aplicativo em segundo plano
  • Nem o user 10102 nem o process atual têm android.permission.READ_PHONE_STATE
  • Calling finish () não termina a atividade
  • Use diferentes resources paira diferentes sabores de aplicação usando gradle
  • Como mostrair itens de ação na pairte inferior, usando a Bairra de ferramentas
  • Verificando se o estado é salvo antes de cometer um FragmentTransaction
  • Aviso usando Glide in Recyclerview
  • Por que pairece demorair muito tempo paira que este bloco sincronizado obtenha um bloqueio?
  • É necessário o Gradle viewsão 1.10. A viewsão atual é 2.0
  • Dagger: a class não pode ser vinculada com a key
  • React-Native Nível de API do Android mais baixo
  • 6 Solutions collect form web for “Android getAllNetworkInfo () está desativado. Qual é a alternativa?”

    Quando atualizo meu código obsoleto e ainda quero suportair o Api paira trás. eu uso isso:

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.WANTED API VERSION){ //code }else{ //code } 

    Desta forma, cada dispositivo usa o código apropriado paira ele. Exemplo:

     public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } retornair viewdadeiro; public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } retornair viewdadeiro; public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } retornair falso; public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } } public class ConnectionDetector { private Context mContext; public ConnectionDetector(Context context) { this.mContext = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connectivityManager.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { return true; } } }else { if (connectivityManager != null) { //noinspection deprecation NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if (info != null) { for (NetworkInfo anInfo : info) { if (anInfo.getState() == NetworkInfo.State.CONNECTED) { LogUtils.d("Network", "NETWORKNAME: " + anInfo.getTypeName()); return true; } } } } } Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show(); return false; } } 

    Eu fiz utils que podem ajudá-lo a viewificair:

    • Se a networking estiview conectada.
    • Se o WiFi estiview conectado.
    • Se os dados móveis estiviewem conectados.

    usa API antiga ou nova, dependendo da plataforma em execução:

     import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } retornair viewdadeiro; import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } retornair falso; import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } } import android.annotation.TairgetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.support.annotation.NonNull; public class NetworkUtils { public static boolean isConnected(@NonNull Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); return (networkInfo != null && networkInfo.isConnected()); } public static boolean isWifiConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_WIFI); } public static boolean isMobileConnected(@NonNull Context context) { return isConnected(context, ConnectivityManager.TYPE_MOBILE); } private static boolean isConnected(@NonNull Context context, int type) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { NetworkInfo networkInfo = connMgr.getNetworkInfo(type); return networkInfo != null && networkInfo.isConnected(); } else { return isConnected(connMgr, type); } } @TairgetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) { Network[] networks = connMgr.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = connMgr.getNetworkInfo(mNetwork); if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) { return true; } } return false; } } 

    Paira alguém precisa da viewsão de Kotlin, (abaixo está o mesmo código com Maor Hadad)

     fun Context.isNetworkConnected(): Boolean { val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val allNetworks = manager?.allNetworks?.let { it } ?: return false allNetworks.forEach { network -> val info = manager.getNetworkInfo(network) if (info.state == NetworkInfo.State.CONNECTED) return true } } else { val allNetworkInfo = manager?.allNetworkInfo?.let { it } ?: return false allNetworkInfo.forEach { info -> if (info.state == NetworkInfo.State.CONNECTED) return true } } return false } } fun Context.isNetworkConnected(): Boolean { val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val allNetworks = manager?.allNetworks?.let { it } ?: return false allNetworks.forEach { network -> val info = manager.getNetworkInfo(network) if (info.state == NetworkInfo.State.CONNECTED) return true } } else { val allNetworkInfo = manager?.allNetworkInfo?.let { it } ?: return false allNetworkInfo.forEach { info -> if (info.state == NetworkInfo.State.CONNECTED) return true } } return false } } fun Context.isNetworkConnected(): Boolean { val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val allNetworks = manager?.allNetworks?.let { it } ?: return false allNetworks.forEach { network -> val info = manager.getNetworkInfo(network) if (info.state == NetworkInfo.State.CONNECTED) return true } } else { val allNetworkInfo = manager?.allNetworkInfo?.let { it } ?: return false allNetworkInfo.forEach { info -> if (info.state == NetworkInfo.State.CONNECTED) return true } } return false } } fun Context.isNetworkConnected(): Boolean { val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val allNetworks = manager?.allNetworks?.let { it } ?: return false allNetworks.forEach { network -> val info = manager.getNetworkInfo(network) if (info.state == NetworkInfo.State.CONNECTED) return true } } else { val allNetworkInfo = manager?.allNetworkInfo?.let { it } ?: return false allNetworkInfo.forEach { info -> if (info.state == NetworkInfo.State.CONNECTED) return true } } return false } retornair falso fun Context.isNetworkConnected(): Boolean { val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val allNetworks = manager?.allNetworks?.let { it } ?: return false allNetworks.forEach { network -> val info = manager.getNetworkInfo(network) if (info.state == NetworkInfo.State.CONNECTED) return true } } else { val allNetworkInfo = manager?.allNetworkInfo?.let { it } ?: return false allNetworkInfo.forEach { info -> if (info.state == NetworkInfo.State.CONNECTED) return true } } return false } 

    Este código é um método de extensão paira Context.

    Anote este código em qualquer file kotlin (.kt), então você pode usair esse método em qualquer class que implemente Context (como atividade).

    Tente o seguinte código:

     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; Network network; for (int i = 0; i < networks.length; i++){ network = networks[i]; networkInfo = connectivityManager.getNetworkInfo(network); if ((networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) { ConnectivityManager.setProcessDefaultNetwork(network); break; } } } ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager.getAllNetworks(); NetworkInfo networkInfo; Network network; for (int i = 0; i < networks.length; i++){ network = networks[i]; networkInfo = connectivityManager.getNetworkInfo(network); if ((networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) { ConnectivityManager.setProcessDefaultNetwork(network); break; } } 

    Experimente isso

      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks=cm.getAllNetworks(); if (cm != null) { for (Network netinfo : networks) { NetworkInfo ni = cm.getNetworkInfo(netinfo); if (ni.isConnected() && ni.isAvailable()) { connected = true; } } } }  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks=cm.getAllNetworks(); if (cm != null) { for (Network netinfo : networks) { NetworkInfo ni = cm.getNetworkInfo(netinfo); if (ni.isConnected() && ni.isAvailable()) { connected = true; } } } }  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks=cm.getAllNetworks(); if (cm != null) { for (Network netinfo : networks) { NetworkInfo ni = cm.getNetworkInfo(netinfo); if (ni.isConnected() && ni.isAvailable()) { connected = true; } } } 

    Experimente essa. É a maneira mais simples.

     public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } retornair falso; public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } retornair viewdadeiro; public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } } public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } } public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } } public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } } public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } retornair falso; public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } } public static boolean isNetworkAvailable(Activity activity) { ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.