我的应用程序中有代码可以检测Wi-Fi是否已主动连接。如果启用了飞行模式,该代码会触发RuntimeException。无论如何,我想在此模式下显示单独的错误消息。如何可靠地检测Android设备是否处于飞行模式?
答案 0 :(得分:126)
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
答案 1 :(得分:92)
通过扩展Alex的答案以包括SDK版本检查,我们有:
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
答案 2 :(得分:47)
如果您不想轮询飞行模式是否处于活动状态,您可以为SERVICE_STATE意图注册BroadcastReceiver并对其做出反应。
在ApplicationManifest(Android 8.0之前版本)中:
<receiver android:enabled="true" android:name=".ConnectivityReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
或以编程方式(所有Android版本):
IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
};
context.registerReceiver(receiver, intentFilter);
如其他解决方案中所述,您可以在收到通知接收器时轮询飞行模式并抛出异常。
答案 3 :(得分:18)
注册飞行模式BroadcastReceiver
(@saxos答案)时,我认为让飞机模式设置的状态远离Intent Extras
是很有意义的,以避免调用{ {1}}或Settings.Global
:
Settings.System
答案 4 :(得分:7)
来自here:
public static boolean isAirplaneModeOn(Context context){
return Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,
0) != 0;
}
答案 5 :(得分:4)
为了摆脱折旧投诉(当针对API17 +并且没有过多关注向后兼容性时),必须与Settings.Global.AIRPLANE_MODE_ON
进行比较:
/**
* @param Context context
* @return boolean
**/
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0);
}
考虑降低API时:
/**
* @param Context context
* @return boolean
**/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings({ "deprecation" })
private static boolean isAirplaneModeOn(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
/* API 17 and above */
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
} else {
/* below */
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
}
答案 6 :(得分:1)
清单代码:
<receiver android:name=".airplanemodecheck" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"></action>
</intent-filter>
</receiver>
Java代码:广播接收器java文件
if(Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0)== 0)
{
Toast.makeText(context, "AIRPLANE MODE Off", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "AIRPLANE MODE On", Toast.LENGTH_SHORT).show();
}
Java代码:活动java文件
在应用程序打开时注册广播接收器如果您在访问互联网等时打开或关闭检查飞行模式的活动时仅采取行动,则无需在清单中添加代码
airplanemodecheck reciver;
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
reciver = new airplanemodecheck();
registerReceiver(reciver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(reciver);
}
Java代码:广播接收器java文件
if(Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0)== 0)
{
Toast.makeText(context, "AIRPLANE MODE Off", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "AIRPLANE MODE On", Toast.LENGTH_SHORT).show();
}
答案 7 :(得分:1)
从API级别 - 17
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
答案 8 :(得分:0)
我写了这个可能有用的课程。它不会直接返回布尔值来告诉您飞行模式是启用还是禁用,但它会在飞机模式从一个更改为另一个时通知您。
public abstract class AirplaneModeReceiver extends BroadcastReceiver {
private Context context;
/**
* Initialize tihe reciever with a Context object.
* @param context
*/
public AirplaneModeReceiver(Context context) {
this.context = context;
}
/**
* Receiver for airplane mode status updates.
*
* @param context
* @param intent
*/
@Override
public void onReceive(Context context, Intent intent) {
if(Settings.System.getInt(
context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0
) == 0) {
airplaneModeChanged(false);
} else {
airplaneModeChanged(true);
}
}
/**
* Used to register the airplane mode reciever.
*/
public void register() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
context.registerReceiver(this, intentFilter);
}
/**
* Used to unregister the airplane mode reciever.
*/
public void unregister() {
context.unregisterReceiver(this);
}
/**
* Called when airplane mode is changed.
*
* @param enabled
*/
public abstract void airplaneModeChanged(boolean enabled);
}
<强>用法强>
// Create an AirplaneModeReceiver
AirplaneModeReceiver airplaneModeReceiver;
@Override
protected void onResume()
{
super.onResume();
// Initialize the AirplaneModeReceiver in your onResume function
// passing it a context and overriding the callback function
airplaneModeReceiver = new AirplaneModeReceiver(this) {
@Override
public void airplaneModeChanged(boolean enabled) {
Log.i(
"AirplaneModeReceiver",
"Airplane mode changed to: " +
((active) ? "ACTIVE" : "NOT ACTIVE")
);
}
};
// Register the AirplaneModeReceiver
airplaneModeReceiver.register();
}
@Override
protected void onStop()
{
super.onStop();
// Unregister the AirplaneModeReceiver
if (airplaneModeReceiver != null)
airplaneModeReceiver.unregister();
}
答案 9 :(得分:0)
这是唯一对我有用的东西(API 27):
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
br
是你的BroadcastReceiver。我相信,最近对权限进行了更改,现在需要ConnectivityManager.CONNECTIVITY_ACTION
和Intent.ACTION_AIRPLANE_MODE_CHANGED
。
答案 10 :(得分:0)
自从Jelly Bean(构建代码17)以来,此字段已移至“全局”设置。 因此,为了获得最佳的兼容性和鲁棒性,我们必须同时考虑这两种情况。 以下示例是用Kotlin编写的。
fun isInAirplane(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.Global.getInt(
context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0
)
} else {
Settings.System.getInt(
context.contentResolver, Settings.System.AIRPLANE_MODE_ON, 0
)
} != 0
}
注意:如果您不支持Jelly Bean之前的版本,则可以省略if子句。
引用Settings.System.AIRPLANE_MODE_ON
时获得的值与在以下位置找到的值相同全球。*
/**
* @deprecated Use {@link android.provider.Settings.Global#AIRPLANE_MODE_ON} instead
*/
@Deprecated
public static final String AIRPLANE_MODE_ON = Global.AIRPLANE_MODE_ON;
这是先前代码的软糖豆版本。
fun isInAirplane(context: Context): Boolean {
return Settings.Global.getInt(
context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0
) != 0
}
答案 11 :(得分:-4)
您可以检查互联网是否已开启
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
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;
}
}