简体   繁体   English

在android 4.4 +中禁用蜂窝广播

[英]Disable cellular radio in android 4.4 +

How can I disable/enable cellular network in android? 如何在Android中禁用/启用蜂窝网络? I need to completly disable it for a short time, and since I have android 4.4+ airplane mode is hard to use. 我需要在短时间内完全禁用它,并且由于我使用的是Android 4.4+飞行模式,因此很难使用。

Is it possible to write code disabling only cellular network? 是否可以编写仅禁用蜂窝网络的代码?

Controlling the Airplane Mode 控制飞行模式

Check to see if it is enabled or not: 检查是否已启用:

boolean isEnabled = Settings.System.getInt(
      context.getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON, 0) == 1;

To toggle it: 要切换它:

// 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);

And you can use this function: 您可以使用以下功能:

/* 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

You need the permission android.permission.WRITE_SETTINGS 您需要权限android.permission.WRITE_SETTINGS

Read more at: 阅读更多信息:

http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html

How to disable GSM connection in Android programmatically 如何以编程方式在Android中禁用GSM连接

EDIT: I read that this method is not longer possible to use Android 4.2 and above. 编辑:我读到此方法不再可以使用Android 4.2及更高版本。 But I think that you can read some interesting code that works in: 但我认为您可以阅读一些有趣的代码,这些代码可用于:

How to turn on/off airplane mode, even on new Android versions (and even with root)? 如何在新的Android版本(甚至root用户)上打开/关闭飞行模式?

Toggle airplane mode in Android 在Android中切换飞行模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM