Android getWifiState 到字符串
我正在尝试获取 Android 设备当前 WiFi 状态的字符串表示形式。我正在使用 WiFiManager
的 getWifiState()
方法,并根据当前状态获取返回的整数。这是完全有意义的,因为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道您已经接受了答案,但您不应该使用发布的代码,因为正如您所说,它很脆弱且难以维护。没有理由会这样。
这可以保护您免受常量分配更改的影响,在 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.
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.
怎么样:
How about just: