有没有比这更好的方法?安卓系统检测是否有夜晚
Boolean isNight;
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour < 6 || hour > 18){
isNight = true;
} else {
isNight = false;
}
它只是检查小时是否在7pm到5am之间。有没有其他方法可以做到这一点?
有没有比这更好的方法?安卓系统检测是否有夜晚
Boolean isNight;
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour < 6 || hour > 18){
isNight = true;
} else {
isNight = false;
}
它只是检查小时是否在7pm到5am之间。有没有其他方法可以做到这一点?
的差别不是很大,但可以进行分配:如果您使用UiModeManager使CarMode模式和夜间模式设置为AUTO
isNight = hour < 6 || hour > 18;
我目前正在这样做,但我认为这太不准确了,特别是当你接近极点时。理想情况下,您需要查看小时,纬度和月份(可能还有某些特定国家/地区的特殊规则)。 编辑:或者有夏季和冬季的默认设置,并让用户调整它们。 –
。您可以通过声明属性在运行时查找来获取当前主题的引用。
attrs.xml
<resources>
<!-- Theme name reference -->
<attr name="metaThemeName" format="string"/>
</resources>
值晚 /themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="metaThemeName">night</item>
</style>
</resources>
的java.class
...
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(R.attr.metaThemeName, outValue, true);
boolean isNight="night".equals(outValue.string);
...
值 \ values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isNightMode">false</bool>
</resources>
值晚 \ values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isNightMode">true</bool>
</resources>
而现在,检查一下你对自己class.java
public boolean isNightMode(){
return getResources().getBoolean(R.bool.isNightMode);
}
嗯,你可以使用该设备的摄像头,以评估周围的亮度/黑暗度,并结合测量GPS获取位置定位所需的时间作为提示用户是否真的在户外......不,我不是认真的:)一个简单的解决方案,就像你当前的一个(禁止一些明显的修复,比如使用'b oolean'和摆脱'如果')最有可能是最好的,你可以得到。 – Xion
没有办法检测设备当前是否处于NightMode模式? 我看到有一种方法来改变设备模式,但我不知道如何读取当前模式。 http://developer.android.com/reference/android/app/UiModeManager.html – OlivierM
你找到了这个答案吗?我正试图解决同样的问题。 UiModeManager似乎不是很有帮助。 –