Android 短信列表的时间显示

来源:互联网 发布:卸载office2016软件 编辑:程序博客网 时间:2024/05/10 08:52

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

Java代码  收藏代码
  1. public static String formatTimeStampString(Context context, long when) {  
  2.         return formatTimeStampString(context, when, false);  
  3.     }  

 

Java代码  收藏代码
  1. public static String formatTimeStampString(Context context, long when, boolean fullFormat) {  
  2.         Time then = new Time();  
  3.         then.set(when);  
  4.         Time now = new Time();  
  5.         now.setToNow();  
  6.   
  7.         // Basic settings for formatDateTime() we want for all cases.  
  8.         int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |  
  9.                            DateUtils.FORMAT_ABBREV_ALL |  
  10.                            DateUtils.FORMAT_CAP_AMPM;  
  11.   
  12.         // If the message is from a different year, show the date and year.  
  13.         if (then.year != now.year) {  
  14.             format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;  
  15.         } else if (then.yearDay != now.yearDay) {  
  16.             // If it is from a different day than today, show only the date.  
  17.             format_flags |= DateUtils.FORMAT_SHOW_DATE;  
  18.         } else {  
  19.             // Otherwise, if the message is from today, show the time.  
  20.             format_flags |= DateUtils.FORMAT_SHOW_TIME;  
  21.         }  
  22.   
  23.         // If the caller has asked for full details, make sure to show the date  
  24.         // and time no matter what we've determined above (but still make showing  
  25.         // the year only happen if it is a different year from today).  
  26.         if (fullFormat) {  
  27.             format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);  
  28.         }  
  29.   
  30.         return DateUtils.formatDateTime(context, when, format_flags);  
  31.     }  

 

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

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

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

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

原创粉丝点击