2014-09-04 6 views
3

Android 4.2부터 비행 모드 켜기/끄기는 일반적인 API를 사용하여 지원되지 않습니다.새로운 안드로이드 버전에서도 (심지어 루트로도) 비행기 모드를 켜고 끄는 방법은 무엇입니까?

WRITE_SECURE_SETTINGS 권한이 부여되면 작동하지만 시스템 응용 프로그램에만 해당됩니다 (읽었을 때).

루트가있는 장치에서 수행하려면 어떻게해야합니까?

비행기 응용 프로그램도 비행기 모드를 전환하려면 루트가 필요합니까? 안드로이드 뿌리 장치 (전화, 태블릿, 주)에 온 오프 비행기/비행 모드를 전환하려면

+0

내 대답은 아래를 참조하십시오. – ChuongPham

+1

[루트를 사용하여 안드로이드 4.2에서 비행기 모드를 토글하는 방법]의 복제본을 만들 수 있습니까? (http://stackoverflow.com/questions/15861046/how-to-toggle-airplane-mode-on-android-4-2-using- 루트) – user1806772

답변

12

, 당신은 다음을 수행 할 수

private final String COMMAND_FLIGHT_MODE_1 = "settings put global airplane_mode_on"; 
private final String COMMAND_FLIGHT_MODE_2 = "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state"; 

@SuppressLint("NewApi") 
@SuppressWarnings("deprecation") 
public void setFlightMode(Context context) { 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { 
     // API 17 onwards. 
     if (isRooted(context)) {    
      int enabled = isFlightModeEnabled(context) ? 0 : 1; 
      // Set Airplane/Flight mode using su commands. 
      String command = COMMAND_FLIGHT_MODE_1 + " " + enabled; 
      executeCommandWithoutWait(context, "-c", command); 
      command = COMMAND_FLIGHT_MODE_2 + " " + enabled; 
      executeCommandWithoutWait(context, "-c", command); 
     } else {     
      try { 
       // No root permission, just show Airplane/Flight mode setting screen. 
       Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(intent); 
      } catch (ActivityNotFoundException e) { 
       Log.e(TAG, "Setting screen not found due to: " + e.fillInStackTrace()); 
      } 
     } 
    } else { 
     // API 16 and earlier. 
     boolean enabled = isFlightModeEnabled(context); 
     Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabled ? 0 : 1); 
     Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
     intent.putExtra("state", !enabled); 
     context.sendBroadcast(intent); 
    } 

은 확인하려면를 비행기/비행 모드 여부 ON과 OFF 이미 다음을 수행하십시오

@SuppressLint("NewApi") 
@SuppressWarnings("deprecation") 
private boolean isFlightModeEnabled(Context context) { 
    boolean mode = false; 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { 
     // API 17 onwards 
     mode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1; 
    } else { 
     // API 16 and earlier. 
     mode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; 
    } 
    return mode; 
} 

su 명령을 실행하려면 다음을 수행하십시오

private void executeCommandWithoutWait(Context context, String option, String command) { 
    boolean success = false; 
    String su = "su"; 
    for (int i=0; i < 3; i++) { 
     // "su" command executed successfully. 
     if (success) { 
      // Stop executing alternative su commands below. 
      break; 
     } 
     if (i == 1) { 
      su = "/system/xbin/su"; 
     } else if (i == 2) { 
      su = "/system/bin/su"; 
     }  
     try { 
      // execute command 
      Runtime.getRuntime().exec(new String[]{su, option, command}); 
     } catch (IOException e) { 
      Log.e(TAG, "su command has failed due to: " + e.fillInStackTrace()); 
     } 
    } 
} 

또는 앱 경우 :

  1. 는 안드로이드 프레임 워크의 인증서로 서명되었다;
  2. /system/app/ 디렉토리에 설치되었습니다. 및
  3. AndroidManifest.xml 파일에 선언 된 관련 태그가 있어야합니다 (예 : WRITE_SECURE_SETTINGS 등).

다음은이 작업을 수행 할 수 있습니다 Settings.Global에 정의 아무것도 할 수

Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, isEnabled ? 0 : 1); 

때문에 읽기 - 쓰기 시스템 앱 - 시스템 응용 프로그램으로 만들어도 타사 응용 프로그램을.

+0

흥미 롭군요, 고마워요. 어떻게 허가를 얻기 위해 "보조금"을 사용하는 것이 효과가 없을까요? 그것을 부여 할 수없는 권한 중 하나입니까? 또한,이 새로운 안드로이드 버전에서 작동하지 않을 수도있는 해결 방법이 있습니까? –

+0

Android 4.2 이상 버전에서 Google은 시스템 앱에서만 사용할 수있는 권한을 제한 했으므로 "grant"가 작동하지 않습니다. 나는 어떤 성공도없이이 길을 이미 시도했다. 내가 게시 한 코드는 대부분의 Android 4.3 및 4.4 루팅 기기에서 작동했습니다. 4.2 이상의 다양한 펌웨어를 실행하는 모든 Android 기기가 없기 때문에 4.2 이상의 루트가있는 Android 기기에서 작동 할 수 있다고 주장 할 수는 없습니다. 그러나 현재까지 뿌리를 달린 4.2+ 기기에서 비행기/비행 모드를 켜는 데 오류가 있다는 사용자는 없습니다. 그래서 좋은 소식은 없다고 생각합니다! ;) – ChuongPham

+0

나는 본다. Android L에서도 작동합니까? 이 솔루션을 어떻게 얻었습니까? 또한 앱이 무엇입니까? 내 것은이 것입니다 (이 기능을 추가 할 생각은 없습니다. 흥미 롭습니다). https://play.google.com/store/apps/details?id=com.lb.app_manager –