コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Android 端末で機内モードかどうか確認する方法

概要

using UnityEngine;

namespace Kogane
{
    public static class AirplaneModeChecker
    {
        public static bool IsAirplaneMode
        {
            get
            {
#if UNITY_EDITOR || UNITY_ANDROID
                if ( Application.isEditor ) return false;

                using var unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" );

                var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" );
                var settingsGlobal  = new AndroidJavaClass( "android.provider.Settings$Global" );

                var airplaneMode = settingsGlobal.CallStatic<int>( "getInt", currentActivity.Call<AndroidJavaObject>( "getContentResolver" ), "airplane_mode_on" );

                return airplaneMode == 1;
#else
                return false;
#endif
            }
        }
    }
}
using Kogane;
using UnityEngine;

public class Example : MonoBehaviour
{
    private void Update()
    {
        Debug.Log( AirplaneModeChecker.IsAirplaneMode );
    }
}

参考サイト様