Android是否提供用于确定周末的语言环境的API? [英] Does Android provide an API for determining weekend days that is locale-aware?

查看:117
本文介绍了Android是否提供用于确定周末的语言环境的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为知道工作日和周末的应用程序提供一些样式,但是我需要它能够感知区域设置.我知道Calendar#getFirstDayOfWeek,但这只会返回一周的第一天,而不是工作周的第一天.

I want to provide some styling to an app that is aware of work days and weekends, but I need it to be locale-aware. I'm aware of Calendar#getFirstDayOfWeek, but this only returns the first day of the week, not the first day of the work week.

例如,对于英语(加拿大),Calendar#getFirstDayOfWeek返回Calendar.SUNDAY,这是我们一周的第一天.我们的工作周从Calendar.MONDAY开始,我们的周末是Calendar.SATURDAYCalendar.SUNDAY.

For example, with English(Canada) Calendar#getFirstDayOfWeek returns Calendar.SUNDAY, which is the first day of our week. Our work week starts on Calendar.MONDAY, and our weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

在英语(英国)中,Calendar#getFirstDayOfWeek返回Calendar.MONDAY,并且他们工作周的第一天是星期一.他们的周末是Calendar.SATURDAYCalendar.SUNDAY.

In English(United Kingdom), Calendar#getFirstDayOfWeek returns Calendar.MONDAY, and the first day of their work-week is Monday. Their weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

对于像希伯来语这样的语言环境,事情变得棘手.他们一周的第一天是Calendar.SUNDAY,而不是他们工作周的第一天是Calendar.MONDAY,而是Calendar.SUNDAY.他们的周末是Calendar.FRIDAYCalendar.SATURDAY.埃及是一样的.

Things get tricky for locales like Hebrew. The first day of their week is Calendar.SUNDAY, but instead of the first day of their work week being Calendar.MONDAY, it's Calendar.SUNDAY. Their weekend days are Calendar.FRIDAY and Calendar.SATURDAY. Egypt is the same.

伊朗只有一个周末,Calendar.FRIDAY.

Android中是否有确定周末的方法?我们有一些适用于大多数西方情况的逻辑,但是它很脆弱,并且可以识别语言环境的平台API是理想的选择,但是我不知道.

Is there any method in Android to determine weekend days? We have some logic in place that covers most Western cases, but it's fragile, and a locale-aware platform API would be ideal, but I'm not aware of one.

请注意,我并不是要对某些最佳"库或任何类似内容提出建议.我要问的是,Android平台上是否存在任何此类语言环境数据,如果存在,则存在哪些API调用来访问此数据.

Please note, I am not asking for a recommendation on some "best" library or anything of the sort. I'm asking if any such locale data exists on the Android platform at all, and if so, what API calls exist to access this data.

推荐答案

Awareness API中有新发布的快照API和篱笆API,用于您的时间区域设置感知API的用例.

There are newly released snapshot and fence APIs in Awareness API for your use-case for a locale-aware API for time.

请查看

Please take a look at Awareness.SnapshotApi.getTimeIntervals which returns a TimeIntervalsResult. You can call getTimeIntervals() method to return an object that has information about the properties of the current time at current locale (specifically the weekend and holidays that you have asked for).

例如,以下代码段将在快照api调用时检查是否在设备区域设置的周末.

For example, the following code snippet checks if it is a weekend at the device's locale at the time of the snapshot api call.

Awareness.SnapshotApi.getTimeIntervalsmGoogleApiClient)
    .setResultCallback(new ResultCallback<TimeIntervalsResult>() {
        @Override
        public void onResult(@NonNull TimeIntervalsResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.e(TAG, "Could not get the time intervals");
                return;
            }
            if (result.getTimeIntervals().hasTimeInterval(
                TimeFence.TIME_INTERVAL_WEEKEND)) {
                // do your custom action, this means it is weekend at the 
                // device locale at this time
            }
        }

如果您希望您的应用在下一个周末或假日在设备区域设置中获得回调,则可以使用新的TimeFence Api

If you'd like your app to get a callback on the next weekend or holiday at the device locale, you can use the new TimeFence Api inTimeInterval(). For example, borrowing the code snippet from the developer docs,

// Create TimeFence
AwarenessFence weekendHolidayFence = Awareness.or(
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_WEEKEND),
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_HOLIDAY))

Awareness.FenceApi.updateFences(
    mGoogleApiClient,
    new FenceUpdateRequest.Builder()
        .addFence("fenceKey", weekendHolidayFence, myPendingIntent)
        .build())
    .setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess()) {
                Log.i(TAG, "Fence was successfully registered.");
            } else {
                Log.e(TAG, "Fence could not be registered: " + status);
            }
        }
    });

这篇关于Android是否提供用于确定周末的语言环境的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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