当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程之短信列表的时间显示实例分析

Android编程之短信列表的时间显示实例分析

2019年07月24日  | 移动技术网IT编程  | 我要评论

沂南论坛,伊素婉,类似浣熊帮帮忙

本文实例讲述了android编程之短信列表的时间显示。分享给大家供大家参考,具体如下:

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;

综合考虑下来,这样的显示设计还是很合理的

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网