android - 设置飞行模式并不完全有效

标签 android

我编写了以下代码,将手机设置为飞行模式以节省电量。这些设备被用作 WiFi 热点,以中继来自印度尼西亚一个村庄的一些传感器的数据。传感器同时发送数据,所以我只需要在午夜退出飞行模式五分钟,然后重新进入飞行模式。

问题是蜂窝 radio 没有关闭,飞机图标也没有出现。尽管电话报告其状态为 airplane_mode on,但仍然可以调用它。市场上的其他小部件似乎也好不到哪儿去。我试过“飞行模式 Wi-Fi 工具”。它也无法让飞机图标出现,也无法禁用手机 radio 。在使用设备设置进入飞行模式的同时观看 LogCat 时,我可以看到发生的事情比在程序中尝试时要多得多。

如果我在 Droid 上加载我的程序,此代码将按预期工作。 AIRPLANE_MODE_RADIOS 设置为 cell, bluetooth, wifi

有问题的设备是三星 Galaxy 5,I5500 测试:

-Froyo 2.2 构建 FROYO.UYJP2 -Froyo 2.2.1 构建 FROYO.UYJPE

一个有趣的旁注:如果我以编程方式设置飞行模式,然后重新启动设备,它会进入完全飞行模式,拒绝来电等。

其他人是否有关于此设备或其他设备的类似故事?有没有办法专门关闭 cell only?

public static void setAirplaneMode(Context context, boolean status) {

    boolean isAM = Settings.System.getInt(context.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, 0) != 0;

    String radios = Settings.System.getString(context.getContentResolver(),
            Settings.System.AIRPLANE_MODE_RADIOS);

    //This line is reporting all radios affected but annunciator does not seem to think so. Does not show airplane
    Wake.logger("Airplane mode is: " + isAM + " changing to " + status + " For radios: " + radios, false);

    // It appears Airplane mode should only be toggled. Don't reset to
    // current state.
    if (isAM && !status) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 0);
        context.sendBroadcast(intent);
        return;
    }
    if (!isAM && status) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 1);
        context.sendBroadcast(intent);
        return;
    }
}

最佳答案

经典的比特扭曲错误。广播 Intent 中的额外数据参数需要是真/假,而不是 1/0。啊!!!

    intent.putExtra("state", true);  //Not 1!!

一部手机可以用,另一部不能用。现在两者都可以。

关于android - 设置飞行模式并不完全有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086698/

相关文章:

Android:如何在不同设备上绘制相同大小的ImageView?

android - 在 android firebase auth 中注销后如何强制谷歌帐户选择器

android - 代码在真实设备中成功运行,但 mockito 需要但未被调用

c# - 该方法已弃用 "palette.generate"来自 android 支持库

java - 如何通过 JNI 将结构从 C 传回 Java

java - 在 android 中捕获完整的 webView

java - 为什么我们提出接口(interface)改造请求

android - onConnectionSuspended。如何测试?这段代码什么时候运行?

android - Windows 64 位操作系统上的 32 位或 64 位 Android SDK?

Android Chrome 禁用搜索输入表单上的自动建议