如何在Android中检测夏令时转换 [英] How to detect Daylight Saving Time transition in android

查看:487
本文介绍了如何在Android中检测夏令时转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有服务巫婆的android应用,它基于一些时间计算触发了警报。

I have an android app that has a service witch fires an alert based on some time calculation.

问题是时区更改( ie:对于位于法国且从UTC + 1到UTC + 2 的用户)。没有通知应用程序此更改,因此,它会延迟触发警报。

The problem is when the timezone changes (i.e.: For a user who's located France and goes from UTC+1 to UTC+2). The application is not notified about this change and so, it fires the alert with a delay.

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

I have already checked android TimeZone API and know that there are some methods which can help, like:

inDaylightTime(日期时间)

getDSTSavings()

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

IS IT POSSIBLE to know when the change will happen, in order to consider it in my calculation algorithm?


顺便说一句:我已经喜欢从Stackoverflow获得了很多博客和问答,但
并没有帮助:(

BTW: I already checked a lot of blogs and Q/A from Stackoverflow but don't help :(

EDIT(清单文件和接收方类):

这是我的清单:

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

接收器类别:

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


推荐答案

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

Well, here is how I resolved this issue:

1-我保留了Android Manifest的状态(请参见问题)

1- I kept the Android Manifest as it is (see the question)

2-然后我稍微改变了接收器:

2- Then i changed the receiver a bit:

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的使用:

3- Here is the method that calculates the new timezone and the use of 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组合在一起。

That's it. So, the solution was to combine all the ApIs (listed in the question) together.


我希望该解决方案能对其他人有所帮助。如果您有任何疑问,请在评论中发布

I hope that this solution will help other. If you have any question just post it in comment

这篇关于如何在Android中检测夏令时转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆