How to check “Automatic date and time” is enabled or not?

前端 未结 4 1903
鱼传尺愫
鱼传尺愫 2020-12-13 09:45

I need to check whether \"Automatic date and time\" in the android device is enabled or not. If it is not enabled I need to display the popup that it is not enabled.

<
相关标签:
4条回答
  • 2020-12-13 10:28
    public static boolean isTimeAutomatic(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
        } else {
            return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 10:39

    I would just like to point out that it is possible to cheat (I just did it on a Samsung Galaxy S4, Android 5.0.1):

    • Disable auto time
    • Disconnect from the internets (airplane mode, no wifi etc...)
    • reboot phone (otherwise the real time is retrieved)
    • set auto time on

    Done today:

    Unix time: 1129294173

    Date: 14102005024933

    is automatic? 1 (using android.provider.Settings.Global.getInt(getActivity().getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0))

    And today definitely isn't Oct 14, 2005

    0 讨论(0)
  • 2020-12-13 10:39
    if(Settings.Global.getInt(getContentResolver(), Global.AUTO_TIME) == 1)
    {
        // Enabled
    }
    else
    {
        // Disabed
    }
    
    0 讨论(0)
  • 2020-12-13 10:44

    Link for API 17 and above

    android.provider.Settings.Global.getInt(getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0);
    

    Link for API 16 and below

    android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0);
    
    0 讨论(0)
提交回复
热议问题