Android 短信列表的时间显示

本文深入探讨了Android系统中短信时间显示的实现原理,包括如何根据当前时间判断并选择合适的时间格式进行展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: http://www.cnblogs.com/flyme/archive/2011/06/30/2094415.html

 

Android 中短信的时间的显示做的很精细。首先,保存在短信数据库 mmssms.db 中的短信时间都是 Long 型的数字。当查询动作结束时,取到这个值之后,会做转换,具体转换的动作在MessageUtils.java的formatTimeStampString函数中完成。

public static String formatTimeStampString(Context context, long when) {
        return formatTimeStampString(context, when, false);
    }

 

public static String formatTimeStampString(Context context, long when, boolean fullFormat) {
        Time then = new Time();
        then.set(when);
        Time now = new Time();
        now.setToNow();

        // Basic settings for formatDateTime() we want for all cases.
        int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |
                           DateUtils.FORMAT_ABBREV_ALL |
                           DateUtils.FORMAT_CAP_AMPM;

        // If the message is from a different year, show the date and year.
        if (then.year != now.year) {
            format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
        } else if (then.yearDay != now.yearDay) {
            // If it is from a different day than today, show only the date.
            format_flags |= DateUtils.FORMAT_SHOW_DATE;
        } else {
            // Otherwise, if the message is from today, show the time.
            format_flags |= DateUtils.FORMAT_SHOW_TIME;
        }

        // If the caller has asked for full details, make sure to show the date
        // and time no matter what we've determined above (but still make showing
        // the year only happen if it is a different year from today).
        if (fullFormat) {
            format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
        }

        return DateUtils.formatDateTime(context, when, format_flags);
    }

 

从第二个具体实现的函数可以看出来,Android是根据当前的时间为比较的依据来决定显示的时间格式:

        1. 如果当前的短信时间中年份跟手机当前的年份不一致,则显示年月日,不显示具体的几点几分,如:2010-6-30

        2. 如果短信的时间跟手机当前时间在同一年,但不是同一天,则只显示月日,如:6月29日

        3.如果是当天的短信,则会计算是上午还是下午的短信,同时显示几点几分记录的该短信,如:下午 12:55

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值