【问题标题】:Enable Flight mode with out disabling the wifi and bluetooth in android在不禁用android中的wifi和蓝牙的情况下启用飞行模式
【发布时间】:2011-08-18 10:10:44
【问题描述】:

我想启用飞行模式,但如果我启用飞行模式,我就无法使用蓝牙、wifi。我的目的是只限制接听电话和短信相关的事情。

我尝试了以下方法,但它不起作用;

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

谁能帮帮我

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以更改激活飞行模式时将关闭的无线电。如果您在激活飞行模式之前执行此操作,则可能只关闭无线电单元。

    注意:更改 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 在以后的安卓版本好像没有效果。
    【解决方案2】:

    据我所知,飞行模式将涵盖禁用所有无线通信的设置选择。

    如果您只想禁用部件,则必须单独完成,而不是通过飞行模式。

    为您希望终止的每个通信部分尝试一种方法。

    【讨论】:

    • 在我的手机(2.2 版本的 HTC Desire)上,在飞行模式下仍然可以访问 WiFi,但蓝牙不可用。
    【解决方案3】:

    您有 2 个可以更改的设置数组:

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

    例如,如果您想防止蓝牙在飞行模式下被禁用, 你可以从airplane_mode_radios 删除蓝牙

    如果您想阻止蓝牙在之前未启用的情况下启用(是的,有时在您打开然后关闭飞行模式时可能会发生) 所以你可以从airplane_mode_toggleable_radios 中删除蓝牙(如果在飞行模式打开然后关闭之前启用它仍然会启用)

    使用亚行:

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

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

    【讨论】:

      【解决方案4】:

      试试这个。我不能保证它在所有设备上都能正常工作。

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

      【讨论】:

      • 这些无线电命令需要 MODIFY_PHONE_STATE 权限,该权限仅授予系统应用程序。
      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      相关资源
      最近更新 更多