Android getAllNetworkInfo()已弃用。 有什么select?

我想使用提供方法getAllNetworkInfo()的连接pipe理器来检查Android中networking的可用性。 此方法在API级别23中已弃用。而且,开发人员文档build议使用getAllNetworks()。 我试过但没有得到我从旧代码中得到的确切function。 请有人可以指导我如何使用getAllNetworks()方法。 以下是我正在使用的代码:

public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { @SuppressWarnings("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; } 

Solutions Collecting From Web of "Android getAllNetworkInfo()已弃用。 有什么select?"

当我更新我弃用的代码,仍然想支持向后的Api。 我使用这个:

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

这样每个设备都使用适当的代码。 例:

 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; } } 

我已经做了可以帮助你检查的utils:

  • 如果networking连接。
  • 如果WiFi连接。
  • 如果移动数据连接。

它使用旧的或新的API取决于运行平台:

 import android.annotation.TargetApi; 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); } } @TargetApi(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; } } 

对于需要Kotlin版本的人来说(下面是与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 } 

此代码是上下文的扩展方法。

在任何kotlin文件(.kt)中记下这个代码,然后你可以在任何实现Context的类(比如Activity)中使用这个方法。

尝试下面的代码:

 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 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; } } } 

试试这个,这是最简单的方法。

 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; } }