sachi
sachi

Reputation: 2132

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

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 ?

Upvotes: 33

Views: 29566

Answers (7)

Dabhi
Dabhi

Reputation: 1

In Kotlin you can use

val localTime = android.provider.Settings.Global.getInt(contentResolver,
    android.provider.Settings.Global.AUTO_TIME,
    0)
if (localTime == 0) {
    val builder = AlertDialog.Builder(this)
    builder.setTitle("Check local time set")
    builder.setMessage("Automatic date and time are not enabled.")
    builder.setPositiveButton("OK") { _, _ ->
        // handle OK button click
    }
    val dialog = builder.create()
    dialog.show()
}

Upvotes: -1

Vanya Rachel
Vanya Rachel

Reputation: 1403

in kotlin you can use

fun isTimeAutomatic(c: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Settings.Global.getInt(c.contentResolver, Settings.Global.AUTO_TIME, 0) == 1
} else {
    Settings.System.getInt(c.contentResolver, Settings.System.AUTO_TIME, 0) == 1
}}

Upvotes: 2

Yasir Tanveer
Yasir Tanveer

Reputation: 121

Check is Auto Time or Zone is Enabled ? (Kotlin)

fun isAutoTimeEnabled(activity: Activity) =
    Settings.Global.getInt(activity.contentResolver, Settings.Global.AUTO_TIME) == 1

fun isAutoTimeZoneEnabled(activity: Activity) =
    Settings.Global.getInt(activity.contentResolver, Settings.Global.AUTO_TIME_ZONE) == 1

Upvotes: 2

Hamidreza Sadegh
Hamidreza Sadegh

Reputation: 2196

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;
    }
}

Upvotes: 40

FloG
FloG

Reputation: 471

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

Upvotes: 8

Carlos Mtz
Carlos Mtz

Reputation: 151

if(Settings.Global.getInt(getContentResolver(), Global.AUTO_TIME) == 1)
{
    // Enabled
}
else
{
    // Disabed
}

Upvotes: 3

velis
velis

Reputation: 10075

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);

Upvotes: 56

Related Questions