如何检测 android 中的夏令时转换
How to detect Daylight Saving Time transition in android
我有一个 android 应用程序,它有一项服务 根据一些时间计算触发警报。
问题出在时区更改时(即:对于位于法国并从 UTC+1 转到 UTC+2).应用程序不会收到有关此更改的通知,因此会延迟触发警报。
我已经检查过 android TimeZone API 并且知道有一些方法可以提供帮助,例如:
是否可以知道更改何时发生,以便在我的计算算法中考虑它?
BTW: I already checked a lot of blogs and Q/A from Whosebug but
don't help :(
编辑(清单文件和接收器 class):
这是我的清单:
<receiver android:name=".receiver.NotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
接收者class:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
Intent service = new Intent(context, NotificationService.class);
service.putExtras(intent);
context.startService(service);
// Timezone or time change
if (intent.getAction() != null
&& (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
|| intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)))
{
Intent i = new Intent(context, DialogPopUpTimeChange.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
好吧,我是这样解决这个问题的:
1- 我保留了 Android 清单(见问题)
2-然后我稍微改变了接收器:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
// Timezone or time change
if (intent.getAction() != null
&& (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
|| intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)))
{
calculateTimeZone(context);
}
}
}
3-这里是计算新时区的方法和DST的使用:
public void calculateTimeZone(Context context) {
float ONE_HOUR_MILLIS = 60 * 60 * 1000;
// Current timezone and date
TimeZone timeZone = TimeZone.getDefault();
Date nowDate = new Date();
float offsetFromUtc = timeZone.getOffset(nowDate.getTime()) / ONE_HOUR_MILLIS;
// Daylight Saving time
if (timeZone.useDaylightTime()) {
// DST is used
// I'm saving this is preferences for later use
// save the offset value to use it later
float dstOffset = timeZone.getDSTSavings() / ONE_HOUR_MILLIS;
// DstOffsetValue = dstOffset
// I'm saving this is preferences for later use
// save that now we are in DST mode
if (timeZone.inDaylightTime(nowDate)) {
// Now you are in use of DST mode
// I'm saving this is preferences for later use
} else {
// DST mode is not used for this timezone
// I'm saving this is preferences for later use
}
}
}
就是这样。因此,解决方案是将所有 ApI(在问题中列出)组合在一起。
I hope that this solution will help other. If you have any question
just post it in comment
我有一个 android 应用程序,它有一项服务 根据一些时间计算触发警报。
问题出在时区更改时(即:对于位于法国并从 UTC+1 转到 UTC+2).应用程序不会收到有关此更改的通知,因此会延迟触发警报。
我已经检查过 android TimeZone API 并且知道有一些方法可以提供帮助,例如:
是否可以知道更改何时发生,以便在我的计算算法中考虑它?
BTW: I already checked a lot of blogs and Q/A from Whosebug but don't help :(
编辑(清单文件和接收器 class):
这是我的清单:
<receiver android:name=".receiver.NotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
接收者class:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
Intent service = new Intent(context, NotificationService.class);
service.putExtras(intent);
context.startService(service);
// Timezone or time change
if (intent.getAction() != null
&& (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
|| intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)))
{
Intent i = new Intent(context, DialogPopUpTimeChange.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
好吧,我是这样解决这个问题的:
1- 我保留了 Android 清单(见问题)
2-然后我稍微改变了接收器:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
// Timezone or time change
if (intent.getAction() != null
&& (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
|| intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)))
{
calculateTimeZone(context);
}
}
}
3-这里是计算新时区的方法和DST的使用:
public void calculateTimeZone(Context context) {
float ONE_HOUR_MILLIS = 60 * 60 * 1000;
// Current timezone and date
TimeZone timeZone = TimeZone.getDefault();
Date nowDate = new Date();
float offsetFromUtc = timeZone.getOffset(nowDate.getTime()) / ONE_HOUR_MILLIS;
// Daylight Saving time
if (timeZone.useDaylightTime()) {
// DST is used
// I'm saving this is preferences for later use
// save the offset value to use it later
float dstOffset = timeZone.getDSTSavings() / ONE_HOUR_MILLIS;
// DstOffsetValue = dstOffset
// I'm saving this is preferences for later use
// save that now we are in DST mode
if (timeZone.inDaylightTime(nowDate)) {
// Now you are in use of DST mode
// I'm saving this is preferences for later use
} else {
// DST mode is not used for this timezone
// I'm saving this is preferences for later use
}
}
}
就是这样。因此,解决方案是将所有 ApI(在问题中列出)组合在一起。
I hope that this solution will help other. If you have any question just post it in comment