티스토리 뷰

App 에서 비행기 모드를 ON / OFF 하는 방법은 간단합니다. 


아래 순서데로 포스팅 하겠습니다.
  1. 비행기 모드 상태값 알아오기.
  2. 비행기 모드 설정하기.
  3. 기타 설정(Manifest) 및 참고.

1. 비행기 모드 상태값 알아오기

 private Boolean isAirModeOn() {
        Boolean isAirplaneMode;
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
            isAirplaneMode = Settings.System.getInt(getContentResolver(),
                                       Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        }else{
            isAirplaneMode = Settings.Global.getInt(getContentResolver(),
                                       Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
        }
        return isAirplaneMode;
}
Settings.System.AIRPLANE_MODE_ON 이 Deprecated 되어 있는 이유는 JellyBean(API 17) 이후로 Settings.System 안에 있던 비행기 모드 관련 메소드들이 Setting.Global 패키지로 이전 되었졌기 때문입니다. 그렇기 때문에 당연히 버전별로 분기처리를 했습니다. 

■ 비행기 모드의 상태값을 가져오는 메서드
static intgetInt(ContentResolver cr, String name, int def)
Convenience function for retrieving a single secure settings value as an integer.
getInt 메서드를 이용하여 현재 비행기 모드의 상태값을 int형으로 리턴 받을 수 있습니다.
리턴값이 0 이면 비행기 모드가 OFF 이며 1 이면 ON 상태 입니다.


2. 비행기 모드 설정하기


private void settingAirplaneMode (boolean mode){
    if (mode){
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
            Settings.System.putInt(getContentResolver(), 
                               Settings.System.AIRPLANE_MODE_ON, 0);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", !mode);
            sendBroadcast(intent);
        }else{

            airplaneModeSettings();
        }
    }else {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
            Settings.System.putInt(getContentResolver(), 
                                   Settings.System.AIRPLANE_MODE_ON, 1);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", !mode);
            sendBroadcast(intent);
        }else{

            airplaneModeSettings();
        }
    }
}
마찬가지로 버전별로 분기 처리 후 putInt 메소드를 이용하여 비행기 모드를 셋팅 할 수 있습니다.
설정 후 sendBroadcast(intent)를 통해서 Device 내에 있는 BroadCast를 Intent와 함께 호출해 주면 putExtra를 통해서 넣은 "state"값이 셋팅되게 됩니다.

■ 비행기 모드를 설정하는 메서드
static booleanputInt(ContentResolver cr, String name, int value)
Convenience function for updating a single settings value as an integer.
비행기 모드를 설정하는 소스를 보면 JellyBean(API 17) 이전 버전에서만 셋팅을 해주는 것을 확인 할 수 있습니다. JellyBean(API 17) 이후로는 비행기 모드를 직접 셋팅 할 수 없으며 환경설정을 통해서만 변경 할 수 있습니다. 
airplaneModeSettings()라는 메소드는 단순히 비행기 모드를 설정 할 수 있는 환경설정 화면을 띄우는 메소드 입니다.


3. 기타 설정(Manifest) 및 참고.


 <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
JellyBean(API 17) 이전에는 WRITE_SETTINGS 퍼미션만 설정해 주면 됩니다.
비행기 모드 설정하는 소스와 같이 코딩 하면 문제가 없으나 JellyBean(API 17) 이후에 비행기 모드 값을 세팅하는 메소드를 넣게 되면 아래와 같은 퍼미션을 선언하라고 나오나 Permission is only granted to system apps 라는 메세지가 뜨면서 선언되지 않습니다.
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
시스템 승인이 안된 퍼미션은 사용 할 수 없으므로 WRITE_SECURE_SETTINGS 퍼미션은 사용 할 수 없습니다. 위에서 말했듯이 이와 같은 문제로 인하여 JellyBean(API 17) 이후로는 비행기 모드를 직접적으로 셋팅 할 수 없습니다.

■ JellyBean(API 17) 변경 사항 중 포스트 내용과 관련된 내용입니다.

New Global Settings

The system settings have been updated to support multiple users with the addition of Settings.Global. This collection of settings is similar to Settings.Secure settings because they are read-only, but applies globally across all user spaces on the device.
Several existing settings were relocated here from either Settings.System or Settings.Secure. If your app is currently making changes to settings previously defined in Settings.System (such as AIRPLANE_MODE_ON), then you should expect that doing so will no longer work on a device running Android 4.2 or higher if those settings were moved to Settings.Global. You can continue to read settings that are in Settings.Global, but because the settings are no longer considered safe for apps to change, attempting to do so will fail silently and the system will write a warning to the system log when running your app on Android 4.2 or higher.


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함