我需要检查Android设备中的“自动日期和时间”是否启用。如果未启用,我需要显示未启用的弹出窗口。
是否可以实现。如果可以的话,如何检查是否启用?
android.provider.Settings.Global.getInt(getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0);
android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0);
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;
}
}
我想指出的是,作弊是有可能的(我刚刚在 Samsung Galaxy S4、Android 5.0.1 上做到了):
今天完成:
Unix时间:1129294173
日期:14102005024933
是自动的吗? 1(使用 android.provider.Settings.Global.getInt(getActivity().getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0))
今天绝对不是 2005 年 10 月 14 日
if(Settings.Global.getInt(getContentResolver(), Global.AUTO_TIME) == 1)
{
// Enabled
}
else
{
// Disabed
}
检查自动时间或区域是否已启用? (科特林)
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
在 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
}}
在 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() }