如何以编程方式禁用 Android 中的 GSM 连接

发布于 2024-12-03 20:11:16 字数 58 浏览 0 评论 0原文

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

I want to enable/disable an Android phone's GSM connection. I need to disable/enable calls and SMS as required. How might I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2024-12-10 20:11:16

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

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

请参阅博客文章Android:控制飞行模式

// 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 权限才能执行此操作。

EDIT: This solution will also turn off wifi, bluetooth, etc...

If you want to turn off radio only, I think it's related to this issue: http://code.google.com/p/android/issues/detail?id=1065
I am really pessimistic about finding a good solution, but curious to see other answers.

See the blog article 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);

where isEnabled is whether airplane mode is enabled or not.

Don't forget you need the WRITE_SETTINGS permission to do this, though.

陌上青苔 2024-12-10 20:11:16
/* 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

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

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

naturally this require the permission android.permission.WRITE_SETTINGS, and for bluetooth android.permission.BLUETOOTH_ADMIN. For NFC you might need android.permission.NFC.

EDITS: heavily modified since original, since I was actually using this in a different way in my own app

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文