Android編程之簡訊列表的時間顯示執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.