如何检测android中的夏令时转换

Sou*_*HDI 4 java timezone android dst

我有一个Android应用程序,有一个服务女巫基于一些时间计算发出警报.

问题是当时区发生变化时(即:对于位于法国并且从UTC + 1变为UTC + 2的用户).应用程序未收到有关此更改的通知,因此会立即触发警报.

我已经检查了android TimeZone API,并且知道有一些方法可以提供帮助,例如:

是否有可能知道何时会发生变化,以便在我的计算算法中考虑它?

顺便说一句:我已经从Stackoverflow检查了很多博客和Q/A,但没有帮助:(

编辑(清单文件和接收器类):

这是我的清单:

<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>
Run Code Online (Sandbox Code Playgroud)

接收者类:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sou*_*HDI 7

好吧,这是我解决这个问题的方法:

1-我保持Android Manifest原样(参见问题)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而已.因此,解决方案是将所有ApI(在问题中列出)组合在一起.

我希望这个解决方案能够帮助其他人.如果您有任何问题,请在评论中发布