【问题标题】:How to disable GSM connection in Android programmatically如何以编程方式在 Android 中禁用 GSM 连接
【发布时间】:2011-11-13 12:23:25
【问题描述】:

我想启用/禁用 Android 手机的 GSM 连接。我需要根据需要禁用/启用通话和短信。我该怎么做?

【问题讨论】:

    标签: android gsm


    【解决方案1】:

    编辑:此解决方案还将关闭 wifi、蓝牙等...

    如果你只想关闭收音机,我认为这与这个问题有关:http://code.google.com/p/android/issues/detail?id=1065 我对找到一个好的解决方案非常悲观,但很想看到其他答案。

    请参阅博客文章Android: Controlling Airplane Mode

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

    isEnabled 表示飞行模式是否启用。

    不过,不要忘记您需要 WRITE_SETTINGS 权限才能执行此操作。

    【讨论】:

    • 当您将手机切换到飞行模式时,您还将抑制 wi-fi 和蓝牙。我不认为这是预期的。主要任务是仅抑制 GSM 连接...
    • 是的。我已经尝试过使用飞机模式。这也将禁用蓝牙。但我只想禁用通话和短信,这可能吗?????我禁用了 GPRS。有什么方法可以用吗????
    • 对不起,你完全正确。我认为没有“适当”的解决方案,请参阅code.google.com/p/android/issues/detail?id=1065,但如果您有聪明的用户,您可以提示他们菜单(输入#*#INFO#*#(#*#4636#*#)在拨号器中)。将编辑我的答案
    【解决方案2】:
    /* 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 和蓝牙android.permission.BLUETOOTH_ADMIN 的权限。对于 NFC,您可能需要 android.permission.NFC

    编辑:自原始版本以来进行了大量修改,因为我实际上在我自己的应用程序中以不同的方式使用它

    【讨论】:

    • 我的手机在 Settings.System.AIRPLANE_MODE_RADIOS: "cell,bluetooth,wifi,nfc,wimax" 中也有 wimax。令人惊讶的是,这款手机不支持 4G WiMax!
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 2012-06-13
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多