如何在 Android 上检测飞行模式?
我的应用程序中有代码可以检测 Wi-Fi 是否已主动连接。如果启用了飞行模式,该代码将触发 RuntimeException。无论如何,我想在这种模式下显示单独的错误消息。如何可靠地检测 Android 设备是否处于飞行模式?
I have code in my application that detects if Wi-Fi is actively connected. That code triggers a RuntimeException if airplane mode is enabled. I would like to display a separate error message when in this mode anyway. How can I reliably detect if an Android device is in airplane mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
通过扩展 Alex 的答案以包括 SDK 版本检查,我们有:
By extending Alex's answer to include SDK version checking we have:
如果您不想轮询飞行模式是否处于活动状态,您可以为 SERVICE_STATE Intent 注册一个 BroadcastReceiver 并对其做出反应。
在您的 ApplicationManifest(Android 8.0 之前)中:
或以编程方式(所有 Android 版本):
并且如其他解决方案中所述,您可以在接收者收到通知时轮询飞行模式并抛出异常。
And if you don't want to poll if the Airplane Mode is active or not, you can register a BroadcastReceiver for the SERVICE_STATE Intent and react on it.
Either in your ApplicationManifest (pre-Android 8.0):
or programmatically (all Android versions):
And as described in the other solutions, you can poll the airplane mode when your receiver was notified and throw your exception.
注册飞行模式
BroadcastReceiver
(@saxos 答案)时,我认为立即从Intent Extras
中按顺序获取飞行模式设置的状态非常有意义避免调用Settings.Global
或Settings.System
:When registering the Airplane Mode
BroadcastReceiver
(@saxos answer) I think it makes a lot of sense to get the state of the Airplane Mode setting right away from theIntent Extras
in order to avoid callingSettings.Global
orSettings.System
:来自此处:
From here :
为了摆脱折旧的抱怨(当针对 API17+ 并且不太关心向后兼容性时),必须与
Settings.Global.AIRPLANE_MODE_ON
进行比较:在考虑较低的 API 时:
in order to get rid of the the depreciation complaint (when targeting API17+ and not caring too much about the backward compatibility), one has to compare with
Settings.Global.AIRPLANE_MODE_ON
:when considering lower API:
在 Oreo 中,请不要使用飞行模式broadCastReceiver。这是一种隐含的意图。它已被删除。以下是当前的例外列表。它当前不在列表中,因此应该无法接收数据。认为它死了。
正如上面另一位用户所说,使用以下代码:
In Oreo please do not use the airplane mode broadCastReceiver. it is an implicit intent. it has been removed. Here is the current exceptions list. its not currently on the list so should fail to receive data. Consider it dead.
as stated by another user above use the following code:
静态广播接收器
清单代码:
Java 代码:广播接收器 java 文件
或
动态广播接收器
Java 代码:活动 java 文件
在应用程序打开时注册广播接收器 如果仅在活动打开时执行操作(例如检查飞行模式),则无需在清单中添加代码当您访问互联网等时打开或关闭
Java 代码:广播接收器 java 文件
Static Broadcast Receiver
Manifest code:
Java code: Broadcast Receiver java file
OR
Dynamic Broadcast Receiver
Java code: Activity java file
Register broadcast receiver on application open no need to add code in manifest if you take an action only when your activity open like check airplane mode is on or off when you access the internet etc
Java code: Broadcast Receiver java file
从 API 级别 - 17
From API Level - 17
自 Jelly Bean(构建代码 17)以来,此字段已移至全局设置。
因此,为了实现最佳的兼容性和鲁棒性,我们必须兼顾这两种情况。
以下示例是用 Kotlin 编写的。
这是之前代码的果冻豆版本。
Since Jelly Bean (Build Code 17), this field has been moved to Global settings.
Thus, to achieve the best compatibility and robustness we have to take care of both cases.
The following example is written in Kotlin.
This is the above-jelly bean version of the previous code.
我写的这门课可能会有帮助。它不会直接返回布尔值来告诉您飞行模式是否已启用或禁用,但当飞行模式从一种更改为另一种时,它会通知您。
使用
I wrote this class that might be helpful. It doesn't directly return a boolean to tell you if Airplane Mode is enabled or disabled, but it will notify you when Airplane Mode is changed from one to the other.
Usage
这是唯一对我有用的东西(API 27):
其中
br
是您的 BroadcastReceiver。我相信,随着最近权限的变化,现在需要ConnectivityManager.CONNECTIVITY_ACTION
和Intent.ACTION_AIRPLANE_MODE_CHANGED
。Here's the only thing what worked for me (API 27):
Where
br
is your BroadcastReceiver. I believe that with the recent changes in permission now bothConnectivityManager.CONNECTIVITY_ACTION
andIntent.ACTION_AIRPLANE_MODE_CHANGED
are needed.您可以检查互联网是否已打开
}
You could check if the internet is on
}