如何查看“自动日期和时间”是否启用?

问题描述 投票:0回答:7

我需要检查Android设备中的“自动日期和时间”是否启用。如果未启用,我需要显示未启用的弹出窗口。

是否可以实现。如果可以的话,如何检查是否启用?

android datetime
7个回答
54
投票

API 17 及以上版本的链接

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

API 16 及以下版本的链接

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

40
投票
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;
    }
}

8
投票

我想指出的是,作弊是有可能的(我刚刚在 Samsung Galaxy S4、Android 5.0.1 上做到了):

  • 禁用自动时间
  • 断开互联网连接(飞行模式、无 WiFi 等...)
  • 重启手机(否则恢复真实时间)
  • 设置自动时间

今天完成:

Unix时间:1129294173

日期:14102005024933

是自动的吗? 1(使用 android.provider.Settings.Global.getInt(getActivity().getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0))

今天绝对不是 2005 年 10 月 14 日


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

2
投票

检查自动时间或区域是否已启用? (科特林)

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

2
投票

在 kotlin 中你可以使用

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

0
投票

在 kotlin 中你可以使用

val localTime = android.provider.Settings.Global.getInt(contentResolver, android.provider.Settings.Global.AUTO_TIME, 0) if (本地时间 == 0) { val builder = AlertDialog.Builder(this) builder.setTitle("检查当地时间设置") builder.setMessage("未启用自动日期和时间。") builder.setPositiveButton("确定") { _, _ -> // 处理确定按钮的点击 } val 对话框 = builder.create() 对话框.show() }

© www.soinside.com 2019 - 2024. All rights reserved.