Search Unity

How to get property from Settings.System

Discussion in 'Android' started by WolveX, Mar 1, 2017.

  1. WolveX

    WolveX

    Joined:
    May 31, 2016
    Posts:
    49
    Hello,

    I need to check if the Airplane mode is currently active, so I found this thread:
    http://stackoverflow.com/questions/4319212/how-can-one-detect-airplane-mode-on-android

    I want to implement it in Unity so I tried the following:

    Code (CSharp):
    1.  
    2.  
    3. public void CheckAirPlaneMethodNew()
    4.     {
    5.         AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings.Global");
    6.         var airplaneMode = systemGlobal.Get<int>("AIRPLANE_MODE_ON");
    7.         Debug.Log("Airplane Mode is Now = " + airplaneMode);
    8.     }
    9.  
    10. public void CheckAirPlaneMethodOld()
    11.     {
    12.         AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings.System");
    13.         var airplaneMode = systemGlobal.Get<int>("AIRPLANE_MODE_ON");
    14.         Debug.Log("Airplane Mode is Now = " + airplaneMode);
    15.     }
    The difference between the two is the class name: android.provider.Settings.Global in newer sdk
    and android.provider.Settings.System in the older ones

    But in both cases I am getting the following error:
    AndroidJavaException: java.lang.ClassNotFoundException: android.provider.Settings.Global
    and
    AndroidJavaException: java.lang.ClassNotFoundException: android.provider.Settings.System

    So I don't know if my code is wrong or if these classes are not included when building APK from Unity

    Any help is welcomed!
     
    Last edited: Mar 2, 2017
  2. WolveX

    WolveX

    Joined:
    May 31, 2016
    Posts:
    49
    Anyone?
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You should look at the docs here: https://developer.android.com/reference/android/provider/Settings.Global.html#AIRPLANE_MODE_ON

    There are a few issues with your code that you should check out:
    1. The string value of the property to check for is 'airplane_mode_on'.
    2. Also, the Settings.Global class was added only in api level 17.
    3. Lastly, Both classes are nested classes (e.g: an inner class inside the Settings class), so the syntax for retrieving them may be different.
    You can try with this code (i wrote it in a text editor, not sure that it'll work):
    Code (CSharp):
    1. public void CheckAirPlaneMethodNew()
    2. {
    3.     // Get context
    4.     using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    5.     {
    6.         var context = actClass.GetStatic<AndroidJavaObject>("currentActivity");
    7.         AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings$Global");
    8.  
    9.         var airplaneMode = systemGlobal.CallStatic<int>("getInt", context.Call<AndroidJavaObject>("getContentResolver"), "airplane_mode_on");
    10.        
    11.         Debug.Log("Airplane Mode is Now = " + airplaneMode);
    12.     }
    13. }
    14.  
    15. public void CheckAirPlaneMethodOld()
    16. {
    17.     // Get context
    18.     using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    19.     {
    20.         var context = actClass.GetStatic<AndroidJavaObject>("currentActivity");
    21.         AndroidJavaClass systemGlobal = new AndroidJavaClass("android.provider.Settings$System");
    22.        
    23.         var airplaneMode = systemGlobal.CallStatic<int>("getInt", context.Call<AndroidJavaObject>("getContentResolver"), "airplane_mode_on");
    24.        
    25.         Debug.Log("Airplane Mode is Now = " + airplaneMode);
    26.     }
    27. }
    Let me know if this helped :)
     
  4. WolveX

    WolveX

    Joined:
    May 31, 2016
    Posts:
    49
    @liortal Thank you very much, it works like a charm!
     
    liortal likes this.