How to check “Automatic date and time” is enabled

2020-02-17 07:03发布

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.

Is it possible to achieve. If possible, how to check enabled or not ?

5条回答
萌系小妹纸
2楼-- · 2020-02-17 07:21

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

查看更多
\"骚年 ilove
3楼-- · 2020-02-17 07:24
if(Settings.Global.getInt(getContentResolver(), Global.AUTO_TIME) == 1)
{
    // Enabled
}
else
{
    // Disabed
}
查看更多
劳资没心,怎么记你
4楼-- · 2020-02-17 07:29
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;
    }
}
查看更多
够拽才男人
5楼-- · 2020-02-17 07:33

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);
查看更多
Melony?
6楼-- · 2020-02-17 07:46

This is mostly done so that users cannot fiddle with the Date/Time of the phone, and is needed for applications like Candy Crush or other licensing apps which should stop working (even when in offline mode) if a particular period of time has elapsed

查看更多
登录 后发表回答