在安卓系统中开启飞行模式时不禁用Wi-Fi和蓝牙

3

我想启用飞行模式,但如果我启用飞行模式,我将无法使用蓝牙、WiFi等功能。我的目的是仅限制接收电话和短信相关事项。

我尝试了以下方法,但它们都没有起作用:

Settings.System.putString(getContentResolver(),
           Settings.System.AIRPLANE_MODE_RADIOS, "cell,bluetooth");

can any one help me in this

4个回答

2

当打开飞行模式时,您可以更改将关闭的无线电。如果在激活飞行模式之前这样做,则可能仅关闭无线电蜂窝。

注意:更改AIRPLANE_MODE_RADIOS会影响系统切换飞行模式按钮的行为。

以下是在Android 2.2上测试过的示例代码。

// Toggle airplane mode.
Settings.System.putInt(context.getContentResolver(),
    Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Change so that only radio cell is turned off
// NOTE: This affects the behavior of the system button for
// toggling air-plane mode. You might want to reset it, in order to
// maintain the system behavior.
Settings.System.putString(context.getContentResolver, 
    Settings.System.AIRPLANE_MODE_RADIOS, "cell"); 

// Post an intent to reload.
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

AIRPLANE_MODE_RADIOS 在后来的 Android 版本中似乎没有效果。 - A.J.

1

你有两个可以更改的设置数组:

airplane_mode_radios=cell,bluetooth,wifi,nfc,wimax
airplane_mode_toggleable_radios=bluetooth,wifi,nfc

例如,如果您想要在飞行模式下禁用蓝牙,请从airplane_mode_radios中移除蓝牙。
如果您想要在切换飞行模式后防止蓝牙被启用(是的,有时切换飞行模式会导致这种情况发生),则可以从airplane_mode_toggleable_radios中移除蓝牙(如果它在飞行模式开启之前已经启用,则仍将被启用)。
使用ADB:
adb shell settings put global airplane_mode_radios 'cell,wifi,nfc,wimax'
adb shell settings put global airplane_mode_toggleable_radios 'wifi,nfc'

使用编程方式:
Settings.Global.putString(getContentResolver(), "airplane_mode_radios", "cell,wifi,nfc,wimax");
Settings.Global.putString(getContentResolver(), "airplane_mode_toggleable_radios", "wifi,nfc");

注意:完成后,请重新启动您的设备


1
据我所知,飞行模式将覆盖选择禁用所有无线通信的设置。
如果您只想禁用某些部分,则必须逐个进行操作,而不能通过飞行模式实现。
尝试针对您希望终止的每个通信部分使用一种方法。

在我的手机上(带有2.2的原装HTC Desire),即使处于飞行模式,WiFi仍然可用,但蓝牙不可用。 - Joubarc

0
尝试这个。我不能保证它在所有设备上都能正常工作。
private ITelephony getTelephonyService() {
    try {
        TelephonyManager oTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        Method mGetITelephony = oTelephonyManager.getClass().getDeclaredMethod("getITelephony", new Class[] {});
        mGetITelephony.setAccessible(true);
        return (ITelephony) mGetITelephony.invoke(oTelephonyManager, new Object[] {});
    } catch (Exception e) {
        return null;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        boolean retval = getTelephonyService().setRadio(false);
        Log.v("Radio", "SetRadio : " + retval);
    } catch (Exception e) {
        Log.v("Radio", Log.getStackTraceString(e));
    }
}

您还需要ITelephony.aidl文件。在您的项目src文件夹下创建一个名为com.android.internal.telephony的包,并在其中创建一个名为ITelephony.aidl的文件,其内容如下:

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony;

import android.os.Bundle;
import java.util.List;

/**
 * Interface used to interact with the phone.  Mostly this is used by the
 * TelephonyManager class.  A few places are still using this directly.
 * Please clean them up if possible and use TelephonyManager insteadl.
 *
 * {@hide}
 */
interface ITelephony {
    /**
     * Check to see if the radio is on or not.
     * @return returns true if the radio is on.
     */
    boolean isRadioOn();

    /**
     * Toggles the radio on or off.
     */
    void toggleRadioOnOff();

    /**
     * Set the radio to on or off
     */
    boolean setRadio(boolean turnOn);
}

1
这些无线电命令需要 MODIFY_PHONE_STATE 权限,该权限仅授予系统应用程序。 - mike47

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接