2011-08-18 110 views
3

我想啓用飛行模式,但如果我啓用飛行模式,我不能夠使用藍牙,WiFi。我的目的是限制只有接聽電話和短信有關的東西。啓用飛行模式與禁用WiFi和藍牙在Android

我試過以下,但它不工作;

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

任何一個可以幫我在這

回答

1

至於IM知道飛行模式將覆蓋設置爲禁用所有無線通信的選擇。

如果您只想禁用部件,則必須單獨完成,而不是通過飛行模式。

爲您希望終止的每部分通信嘗試一種方法。

+0

在我的手機上(股票HTC Desire 2.2),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

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); 
+0

AIRPLANE_MODE_RADIOS似乎在後來的android版本中沒有效果。 –