欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 如何判断当前是否为飞行模式

程序员文章站 2023-12-05 17:37:34
android中如何判断系统当前是否处于飞行模式中: 复制代码 代码如下: public static boolean isairmodeon(context contex...
android中如何判断系统当前是否处于飞行模式中:
复制代码 代码如下:

public static boolean isairmodeon(context context) {
return (settings.system.getint(context.getcontentresolver(),
settings.system.airplane_mode_on, 0) == 1 ? true : false);
}

如何切换飞行模式
复制代码 代码如下:

public static void setairplanemode(context context, boolean enabling) {
settings.system.putint(context.getcontentresolver(),
settings.system.airplane_mode_on, enabling ? 1 : 0);
intent intent = new intent(intent.action_airplane_mode_changed);
intent.putextra("state", enabling);
context.sendbroadcast(intent);
}

如何注册和取消自动飞行时间
注册
复制代码 代码如下:

alarmmanager am = (alarmmanager) context
.getsystemservice(context.alarm_service);
intent intent = new intent(air_alert_action);
parcel out = parcel.obtain();
air.writetoparcel(out, 0);
out.setdataposition(0);
intent.putextra(air_raw_data, out.marshall());
pendingintent sender = pendingintent.getbroadcast(context, 0, intent,
pendingintent.flag_cancel_current);
am.set(alarmmanager.rtc_wakeup, attimeinmillis, sender);取消
alarmmanager am = (alarmmanager) context
.getsystemservice(context.alarm_service);
endingintent sender = pendingintent.getbroadcast(context, 0,
new intent(action), pendingintent.flag_cancel_current);
am.cancel(sender);

如何控制切换飞行模式的硬件(cell,bluetooth,wifi)
复制代码 代码如下:

settings.system.putstring(context.getcontentresolver(),
settings.system.airplane_mode_radios, air_mode_radios);air_mode_radios为一个这样的字符串,看android源码中android/provider/settings.java

/***
* whether airplane mode is on.
*/
public static final string airplane_mode_on = "airplane_mode_on";
/***
* constant for use in airplane_mode_radios to specify bluetooth radio.
*/
public static final string radio_bluetooth = "bluetooth";
/***
* constant for use in airplane_mode_radios to specify wi-fi radio.
*/
public static final string radio_wifi = "wifi";
/***
* constant for use in airplane_mode_radios to specify cellular radio.
*/
public static final string radio_cell = "cell";
/***
* a comma separated list of radios that need to be disabled when airplane mode
* is on. this overrides wifi_on and bluetooth_on, if wi-fi and bluetooth are
* included in the comma separated list.
*/
public static final string airplane_mode_radios = "airplane_mode_radios";
/***
* a comma separated list of radios that should to be disabled when airplane mode
* is on, but can be manually reenabled by the user. for example, if radio_wifi is
* added to both airplane_mode_radios and airplane_mode_toggleable_radios, then wifi
* will be turned off when entering airplane mode, but the user will be able to reenable
* wifi in the settings app.
*
* {@hide}
*/
public static final string airplane_mode_toggleable_radios = "airplane_mode_toggleable_radios";

如果air_mode_radios=“cell,bluetooth,wifi”,这就便是切换飞行模式是切换字符串中的这cell,bluetooth,wifi硬件,我们可以通过设置该字符串的值,来控制这三个硬件是否在切换飞行模式是进行切换状态。