3

我想在设置警报时生成与 Android 警报类似的字符串。

示例:
-“警报设置为从现在起 6 天 10 小时 9 分钟。”
- “闹钟设置为 10 小时 9 分钟后。”
- “从现在开始设置 9 分钟的警报。”

我可以使用:

DateUtils.getRelativeTimeSpanString(alarm.triggerTime, 
                    System.currentTimeMillis(), 
                    DateUtils.MINUTE_IN_MILLIS, 
                    flags).toString();

这会产生类似于"in 19 hours"的消息。我似乎无法制作更像“在 19 小时 5 分钟内”“在 5 天 10 小时 30 分钟内”的东西。

如果可能的话,我想弄清楚如何使用 Android 本地化帮助程序类来实现这一点,例如DateUtils. 我讨厌不得不缝合可能会导致其他语言出现问题的字符串。

4

1 回答 1

-1

如果您正在使用Eclipse IDE,请尝试GrepCode_Plugin从本教程安装GrepCode_Plugin

完成后,GrepCode Search使用android.text.format.DateUtils. 它会列出你所有的文件。尝试下载最新的。

从该文件中获取方法并根据您的需要进行修改。

供您参考,我将把列出的方法放在android 4.1.1_r1

public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution,
            int flags) {
    Resources r = Resources.getSystem();
    boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;

    boolean past = (now >= time);
    long duration = Math.abs(now - time);

    int resId;
    long count;
    if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
        count = duration / SECOND_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_seconds_ago;
            } else {
                resId = com.android.internal.R.plurals.num_seconds_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_seconds;
            } else {
                resId = com.android.internal.R.plurals.in_num_seconds;
            }
        }
    } else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
        count = duration / MINUTE_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_minutes_ago;
            } else {
                resId = com.android.internal.R.plurals.num_minutes_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_minutes;
            } else {
                resId = com.android.internal.R.plurals.in_num_minutes;
            }
        }
    } else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
        count = duration / HOUR_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_hours_ago;
            } else {
                resId = com.android.internal.R.plurals.num_hours_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_hours;
            } else {
                resId = com.android.internal.R.plurals.in_num_hours;
            }
        }
    } else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
        count = getNumberOfDaysPassed(time, now);
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_days_ago;
            } else {
                resId = com.android.internal.R.plurals.num_days_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_days;
            } else {
                resId = com.android.internal.R.plurals.in_num_days;
            }
        }
    } else {
        // We know that we won't be showing the time, so it is safe to pass
        // in a null context.
        return formatDateRange(null, time, time, flags);
    }

    String format = r.getQuantityString(resId, (int) count);
    return String.format(format, count);
}
于 2013-02-08T04:52:51.153 回答