networkinfo和wificonfiguration在sdk 22中已被弃用

guz6ccqo  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(1065)

我在pax a920设备(sdk版本22)中使用android棒棒糖。生成时,我收到如下警告消息:
NetworkInfo in android.net has been deprecated WifiConfiguration in android.net has been deprecated 我很困惑,因为我在中看到了sdk 22的示例 C:\Users\{YOUR_ACCOUNT}\AppData\Local\Android\Sdk\sources\android-22\com\android\connectivitymanagertest 使用该方法访问wifi,但为什么它看起来不受欢迎?
什么方法与替换不推荐的方法相同?

  1. public boolean isWifiConnected() {
  2. boolean isWifiConnected = false;
  3. try {
  4. NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  5. if (networkInfo != null) {
  6. isWifiConnected = networkInfo.getState() == CONNECTED;
  7. }
  8. Log.i(TAG, "wifi adapter is connected? " + isWifiConnected);
  9. } catch (Exception ex) {
  10. ex.printStackTrace();
  11. }
  12. return isWifiConnected;
  13. }
  14. public void removeNetwork() {
  15. List<WifiConfiguration> wifiCfgList = wifiManager.getConfiguredNetworks();
  16. if (wifiCfgList.size() > 0) {
  17. for (WifiConfiguration item : wifiCfgList) {
  18. if (item != null) {
  19. wifiManager.removeNetwork(item.networkId);
  20. wifiManager.saveConfiguration();
  21. }
  22. }
  23. }
  24. }

谢谢你的指点。

z9ju0rcb

z9ju0rcb1#

欢迎来到stackoverflow!
不推荐使用的类已使用 ConnectivityManager 系统服务和 NetworkCallbacks : https://developer.android.com/training/monitoring-device-state/connectivity-status-type
尽管官方代码示例演示了如何获取 NetworkInfo 从…起 ConnectivityManager 有一个突出显示的注解:

下面是一个示例代码,说明如何获取当前网络状态,以及如何随着时间的推移接收更新。这是一个部分精简的解决方案,我将在生产中使用。如果将其与rxjava或rxkotlin连接,则可以创建一个可观察对象,该对象将保存网络状态,并在调用networkcallback的重写方法时进行更新。
关于java的注意事项:公共类级别的变量是为了简洁而公开的。相反,我会为这些变量创建一些getter来访问这些变量背后的值。
请随意提问。
JAVA

  1. class NetworkReachabilityService {
  2. public NetworkType networkType;
  3. public NetworkState networkState = NetworkState.Unavailable;
  4. private ConnectivityManager connectivityManager;
  5. private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
  6. @Override
  7. public void onAvailable(@NonNull Network network) {
  8. super.onAvailable(network);
  9. updateAvailability(connectivityManager.getNetworkCapabilities(network));
  10. }
  11. @Override
  12. public void onLosing(@NonNull Network network, int maxMsToLive) {
  13. super.onLosing(network, maxMsToLive);
  14. networkState = NetworkState.Losing;
  15. }
  16. @Override
  17. public void onLost(@NonNull Network network) {
  18. super.onLost(network);
  19. networkState = NetworkState.Lost;
  20. }
  21. @Override
  22. public void onUnavailable() {
  23. super.onUnavailable();
  24. networkState = NetworkState.Unavailable;
  25. }
  26. @Override
  27. public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
  28. super.onCapabilitiesChanged(network, networkCapabilities);
  29. updateAvailability(networkCapabilities);
  30. }
  31. };
  32. public NetworkReachabilityService(Context context) {
  33. connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  34. }
  35. private void updateAvailability(NetworkCapabilities networkCapabilities) {
  36. if (networkCapabilities == null) {
  37. networkState = NetworkState.Unavailable;
  38. return;
  39. }
  40. if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
  41. networkType = NetworkType.CELL;
  42. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
  43. networkType = NetworkType.WiFi;
  44. }
  45. networkState = NetworkState.Available;
  46. }
  47. public void resumeListeningNetworkChanges() {
  48. pauseListeningNetworkChanges();
  49. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  50. connectivityManager.registerDefaultNetworkCallback(networkCallback);
  51. } else {
  52. connectivityManager.registerNetworkCallback(
  53. new NetworkRequest.Builder().build(),
  54. networkCallback
  55. );
  56. }
  57. }
  58. public void pauseListeningNetworkChanges() {
  59. try {
  60. connectivityManager.unregisterNetworkCallback(networkCallback);
  61. } catch (IllegalArgumentException exception) {
  62. // Usually happens only once if: "NetworkCallback was not registered"
  63. }
  64. }
  65. private enum NetworkState {
  66. Available, Unavailable, Connecting, Losing, Lost
  67. }
  68. private enum NetworkType {
  69. WiFi, CELL, OTHER
  70. }
  71. }

Kotlin

  1. sealed class NetworkState {
  2. data class Available(val type: NetworkType) : NetworkState()
  3. object Unavailable : NetworkState()
  4. object Connecting : NetworkState()
  5. object Losing : NetworkState()
  6. object Lost : NetworkState()
  7. }
  8. sealed class NetworkType {
  9. object WiFi : NetworkType()
  10. object CELL : NetworkType()
  11. object OTHER : NetworkType()
  12. }
  13. class NetworkReachabilityService private constructor(context: Context) {
  14. private val connectivityManager: ConnectivityManager =
  15. context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  16. private val networkCallback = object : ConnectivityManager.NetworkCallback() {
  17. // There are more functions to override!
  18. override fun onLost(network: Network) {
  19. super.onLost(network)
  20. networkState = NetworkState.Lost
  21. }
  22. override fun onUnavailable() {
  23. super.onUnavailable()
  24. networkState = NetworkState.Unavailable
  25. }
  26. override fun onLosing(network: Network, maxMsToLive: Int) {
  27. super.onLosing(network, maxMsToLive)
  28. networkState = NetworkState.Losing
  29. }
  30. override fun onAvailable(network: Network) {
  31. super.onAvailable(network)
  32. updateAvailability(connectivityManager.getNetworkCapabilities(network))
  33. }
  34. override fun onCapabilitiesChanged(
  35. network: Network,
  36. networkCapabilities: NetworkCapabilities
  37. ) {
  38. super.onCapabilitiesChanged(network, networkCapabilities)
  39. updateAvailability(networkCapabilities)
  40. }
  41. }
  42. var networkState: NetworkState = NetworkState.Unavailable
  43. private set
  44. private fun updateAvailability(networkCapabilities: NetworkCapabilities?) {
  45. if (networkCapabilities == null) {
  46. networkState = NetworkState.Unavailable
  47. return
  48. }
  49. var networkType: NetworkType = NetworkType.OTHER
  50. if (networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) {
  51. networkType = NetworkType.CELL
  52. }
  53. if (networkCapabilities.hasTransport(TRANSPORT_WIFI)) {
  54. networkType = NetworkType.WiFi
  55. }
  56. networkState = NetworkState.Available(networkType)
  57. }
  58. fun pauseListeningNetworkChanges() {
  59. try {
  60. connectivityManager.unregisterNetworkCallback(networkCallback)
  61. } catch (e: IllegalArgumentException) {
  62. // Usually happens only once if: "NetworkCallback was not registered"
  63. }
  64. }
  65. fun resumeListeningNetworkChanges() {
  66. pauseListeningNetworkChanges()
  67. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  68. connectivityManager.registerDefaultNetworkCallback(networkCallback)
  69. } else {
  70. connectivityManager.registerNetworkCallback(
  71. NetworkRequest.Builder().build(),
  72. networkCallback
  73. )
  74. }
  75. }
  76. }

开始接收网络状态更新呼叫 resumeListeningNetworkChanges 停止 pauseListeningNetworkChanges 分别地

展开查看全部

相关问题