Created
April 18, 2024 05:29
-
-
Save baba-s/009488f2c2690746f7e6856ae1953584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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