Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created April 18, 2024 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baba-s/009488f2c2690746f7e6856ae1953584 to your computer and use it in GitHub Desktop.
Save baba-s/009488f2c2690746f7e6856ae1953584 to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace Kogane
{
/// <summary>
/// Android 端末の「設定 > システム > 日付と時刻」の「日時を自動的に設定」と「タイムゾーンを自動的に設定」の設定値を取得できるクラス
/// </summary>
public static class AndroidDeviceDateTimeChecker
{
//================================================================================
// プロパティ(static)
//================================================================================
/// <summary>
/// 「日時を自動的に設定」がオンなら true を返します
/// </summary>
public static bool IsAutoTime => Impl( "auto_time" );
/// <summary>
/// 「タイムゾーンを自動的に設定」がオンなら true を返します
/// </summary>
public static bool IsAutoTimeZone => Impl( "auto_time_zone" );
//================================================================================
// 関数(static)
//================================================================================
/// <summary>
/// 指定された設定の値を返します
/// </summary>
private static bool Impl( string name )
{
#if UNITY_EDITOR || UNITY_ANDROID
if ( Application.isEditor ) return false;
using var unityPlayer = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" );
using var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>( "currentActivity" );
using var settingsGlobal = new AndroidJavaClass( "android.provider.Settings$Global" );
using var getContentResolver = currentActivity.Call<AndroidJavaObject>( "getContentResolver" );
return settingsGlobal.CallStatic<int>( "getInt", getContentResolver, name, 0 ) != 0;
#else
return false;
#endif
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment