在Android 4.4 +
中禁用蜂窝无线电如何禁用/启用android中的蜂窝网络?我需要完全禁用它很短的时间,因为我有Android 4.4+飞机模式很难使用。在Android 4.4 +
是否可以编写禁用蜂窝网络的代码?
控制飞行模式
检查,看它是否被启用与否:
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
要切换它:
// toggle airplane mode
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
而且,您可以使用此功能:
/* Toggle airplane mode for 1 of the 4 allowed types
* type allowed values: cell, wifi, bluetooth, nfc
*/
private void changeRadioComponentEnabled(Context context, String type, boolean radio_component_enabled, boolean reset){
// now toggle airplane mode from on to off, or vice versa
Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, radio_component_enabled ? 0 : 1);
// now change system behavior so that only one component is turned off
// this also affects the future behavior of the system button for toggling air-plane mode.
// to reset it in order to maintain the system behavior, set reset to true, otherwise we lazily make sure mobile voice is always on
Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, type);
// post an intent to reload so the menu button switches to the new state we set
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", radio_component_enabled ? false : true);
context.sendBroadcast(intent);
// revert to default system behavior or not
if (reset){ Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell,bluetooth,wifi,nfc"); }
// if reset to default is not chosen, always enable mobile cell at least
// but only if NOT changing the mobile connection...
else if (type.indexOf("cell") == 0) { Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell");}
}//end method
您需要的权限android.permission.WRITE_SETTINGS
更多详情:
http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html
编辑:我看,这种方法是不再可能使用Android 4.2及以上。但我认为,你可以阅读一些有趣的代码,在工作:
How to turn on/off airplane mode, even on new Android versions (and even with root)?
我在之前看到过帖子,但是它给出了'Permission Denial:不允许发送广播android.intent.action.AIRPLANE_MODE'异常,我是否需要root权限才能使用该代码? – user2654072
@ user2654072正如我在答案中所说的,您需要您在manifest.xml中需要的权限'android.permission.WRITE_SETTINGS'这个' –
@ user2654072我编辑答案,看看答案的最后。 –
“飞行模式很难用”怎么难用? – m0skit0