Answers:
/**
* 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;
}
Settings.Global
。
android.intent.action.AIRPLANE_MODE
,因为模式更改需要时间才能完成。检查Intent.ACTION_AIRPLANE_MODE_CHANGED
是否要这样做。
通过扩展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;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
在方法之前添加。
并且,如果您不想轮询“飞行模式”是否处于活动状态,则可以为SERVICE_STATE Intent注册一个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);
而且,如其他解决方案中所述,您可以在收到通知时轮询飞行模式,并抛出异常。
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
/<action android:name="android.intent.action.AIRPLANE_MODE" />
boolean isPlaneModeOn = intent.getBooleanExtra("state", false);
布尔isPlaneModeOn
将true
用户是否已接通的平面模式或者false
如果它被关闭
注册飞行模式BroadcastReceiver
(@saxos回答)时,我认为直接从获取飞行模式设置的状态很有意义,Intent Extras
以避免调用Settings.Global
或Settings.System
:
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
// handle Airplane Mode on
} else {
// handle Airplane Mode off
}
}
为了摆脱折旧投诉(在以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;
}
}
在奥利奥(Oreo)中,请不要使用飞行模式broadCastReceiver。这是一个隐含的意图。它已被删除。这是当前的例外列表。它当前不在列表中,因此应该无法接收数据。认为它死了。
如上面另一个用户所述,使用以下代码:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings({ "deprecation" })
public 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;
}
}
清单代码:
<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();
}
从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;
}
我写了这节课可能会有所帮助。它不会直接返回布尔值来告诉您是启用还是禁用了飞行模式,但是当飞行模式从一种更改为另一种时,它会通知您。
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();
}
这是唯一对我有用的东西(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
需要。
自从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
与在Global下找到的值相同。*
/**
* @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
}
您可以检查互联网是否打开
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;
}
}