Android getWifiState 到字符串

发布于 2024-09-03 07:10:29 字数 559 浏览 2 评论 0原文

我正在尝试获取 Android 设备当前 WiFi 状态的字符串表示形式。我正在使用 WiFiManagergetWifiState() 方法,并根据当前状态获取返回的整数。这是完全有意义的,因为 getWifiState() 应该返回一个整数。该方法的文档表明,可能的返回值会转换为以下常量值之一

  • 0 WIFI_STATE_DISABLING
  • 1 WIFI_STATE_DISABLED
  • 2 WIFI_STATE_ENABLING
  • 3 WIFI_STATE_ENABLED
  • 4 WIFI_STATE_UNKNOWN

有没有一种简单的方法可以将 getWiFiState() 返回的整数转换为字符串表示形式?我看过 getIntExtra 但不确定它的用途。

I am attempting to get the string representation of an Android device's current WiFi state. I am using the getWifiState() method of the WiFiManager and am getting an integer returned depending on the current state. This makes total sense as getWifiState() is supposed to return an integer. The method's documentation indicates that the possible return values translate to one of the following constant values

  • 0 WIFI_STATE_DISABLING
  • 1 WIFI_STATE_DISABLED
  • 2 WIFI_STATE_ENABLING
  • 3 WIFI_STATE_ENABLED
  • 4 WIFI_STATE_UNKNOWN

Is there an easy way to translate the integer returned from getWiFiState() to a string representation? I've looked at getIntExtra but am unsure of its use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-09-10 07:10:29

我知道您已经接受了答案,但您不应该使用发布的代码,因为正如您所说,它很脆弱且难以维护。没有理由会这样。

  public String getWifiStateStr() {
    switch (mWifiManager.getWifiState()) {
      case WifiManager.WIFI_STATE_DISABLING:
        return "disabling";
      case WifiManager.WIFI_STATE_DISABLED:
        return "disabled";
      case WifiManager.WIFI_STATE_ENABLING:
        return "enabling";
      case WifiManager.WIFI_STATE_ENABLED:
        return "enabled";
      case WifiManager.WIFI_STATE_UNKNOWN:
        return "unknown";
      default:
        return null;  //or whatever you want for an error string
    }
  }

这可以保护您免受常量分配更改的影响,在 6 个月内更容易阅读,并且假设您正确处理错误字符串,如果允许的返回值数量增加,应该会限制错误。

当您在 Android 文档中看到一个变量被列为“常量”时,这意味着它被声明为 public static final。它可以作为 ClassName.CONST_VARIABLE_NAME 进行访问。按照惯例,这些变量全部大写,但这不是语言要求。一般来说,您永远不需要使用此类成员的实际值,您应该始终通过成员名称访问它们,除非您需要做一些奇怪的事情。

i know you already accepted an answer but you should not use the code posted because, as you said, it is brittle and difficult to maintain. There is no reason for it to be so.

  public String getWifiStateStr() {
    switch (mWifiManager.getWifiState()) {
      case WifiManager.WIFI_STATE_DISABLING:
        return "disabling";
      case WifiManager.WIFI_STATE_DISABLED:
        return "disabled";
      case WifiManager.WIFI_STATE_ENABLING:
        return "enabling";
      case WifiManager.WIFI_STATE_ENABLED:
        return "enabled";
      case WifiManager.WIFI_STATE_UNKNOWN:
        return "unknown";
      default:
        return null;  //or whatever you want for an error string
    }
  }

This protects you from changes in the constant assignment, will be much easier to read in 6 months, and assuming you handle the error string correctly should limit bugs if the number of allowed return values increases.

When you see a variable listed as a 'Constant' in Android documentation it means that it is declared public static final. It can be accessed as ClassName.CONST_VARIABLE_NAME. It is convention that these variables will be in all caps but it is not a language requirement. Generally speaking you should never need to use the actual value of such members, you should always access them by member name unless you need to do something odd.

傲鸠 2024-09-10 07:10:29

怎么样:

  public String getWifiStateStr() {
    switch (mWifiManager.getWifiState()) {
      case 0:
        return "disabling";
      case 1:
        return "disabled";
      case 2:
        return "enabling";
      case 3:
        return "enabled";
      default:
        return "unknown";
    }
  }

How about just:

  public String getWifiStateStr() {
    switch (mWifiManager.getWifiState()) {
      case 0:
        return "disabling";
      case 1:
        return "disabled";
      case 2:
        return "enabling";
      case 3:
        return "enabled";
      default:
        return "unknown";
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文