Android 禁用安卓4.4+;

Android 禁用安卓4.4+;,android,configuration,airplane,Android,Configuration,Airplane,如何在android中禁用/启用蜂窝网络?我需要在短时间内完全禁用它,因为我有android 4.4+飞行模式很难使用 是否可以编写仅禁用蜂窝网络的代码 控制飞机模式 检查是否已启用: boolean isEnabled = Settings.System.getInt( context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; 要切换它,请执行以下操作: // toggle ai

如何在android中禁用/启用蜂窝网络?我需要在短时间内完全禁用它,因为我有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\u设置

更多信息请访问:

编辑:我了解到,使用Android 4.2及以上版本不再可能使用此方法。但我认为您可以阅读一些有趣的代码:


“飞机模式很难使用”怎么很难使用?我以前看到过这篇文章,但它给出了
权限拒绝:不允许发送广播android.intent.action.franker\u mode
异常,我需要root和权限才能使用该代码吗?@user2654072正如我在回答中所说的,你需要权限
android.permission。在manifest.xml中写下你需要的设置
@user2654072我编辑了答案,看看答案的最后部分。@user2654072你有一些进展吗?目前在无根手机上没有运气,我可能会根它,并试图使系统应用程序,然后可能会发生一些事情。谷歌从普通开发者那里屏蔽了这一点,真是太疯狂了。