Clock display of Android4.4 systemui analysis

Source: Internet
Author: User

The time on the Systemui is displayed as long as it is on the/frameworks/base/packages/systemui/src/com/android/systemui/statusbar/policy/clock.java class

This class is also very simple, listening and processing broadcast

@Overrideprotected void Onattachedtowindow () {Super.onattachedtowindow (); if (!mattached) {mattached = True;i Ntentfilter filter = new Intentfilter (); filter.addaction (Intent.action_time_tick); Filter.addaction (Intent.ACTION_ time_changed); filter.addaction (intent.action_timezone_changed); Filter.addaction (Intent.action_configuration_ CHANGED); filter.addaction (intent.action_user_switched); GetContext (). Registerreceiver (Mintentreceiver, filter, Null,gethandler ());} Note:it's safe to does these after registering the receiver since the//receiver all runs//in the main thread, there Fore the receiver can ' t run before this//method returns.//The time zone may has changed while the receiver wasn ' t Regis tered,//so update the Timemcalendar = Calendar.getinstance (Timezone.getdefault ());/Make sure we update to the current T Imeupdateclock ();} @Overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); if (mattached) {getcontext (). Unregisterreceiver (mintentreceiver); mattached = FalSe;}} 
To update time on the radio

Private final Broadcastreceiver mintentreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (Context Context, Intent Intent) {String action = intent.getaction (); LOG.D (TAG, "action =" + action), if (Action.equals (intent.action_timezone_changed)) {String TZ = Intent.getstringextra (" Time-zone "); Mcalendar = Calendar.getinstance (Timezone.gettimezone (TZ)); if (Mclockformat! = null) { Mclockformat.settimezone (Mcalendar.gettimezone ());} LOG.D (TAG, "Mcalendar =" + mcalendar+ "timezone.gettimezone (TZ) =" + Timezone.gettimezone (TZ));} else if (action.equals (intent.action_configuration_changed)) {final Locale Newlocale = Getresources (). GetConfiguration (). Locale;if (!newlocale.equals (Mlocale)) {Mlocale = Newlocale;mclockformatstring = ""; Force Refresh}}updateclock ();};
In the settings can change the time display is 12 hours or 24 hours

The format composition of time is done in this function

Private final Charsequence Getsmalltime () {Context context = GetContext (); Boolean is24 = Dateformat.is24hourformat ( context); LocaleData d = localedata.get (Context.getresources (). GetConfiguration (). locale); final char MAGIC1 = ' \uef00 '; final Char MAGIC2 = ' \uef01 '; SimpleDateFormat SDF; String format = Is24? D.timeformat24:d.timeformat12;if (!format.equals (mclockformatstring)) {/* * Search for an unquoted ' a ' in the format str ING, so we can add * dummy characters around it to let us find it again after * formatting and change its size. */if (Am_pm_style! = am_pm_style_normal) {int a = -1;boolean quoted = false;for (int i = 0; i < format.length (); i++) { char C = Format.charat (i); if (c = = ' \ ') {quoted =!quoted;} if (!quoted && c = = ' A ') {a = I;break;}} if (a >= 0) {//Move a back so any whitespace before am/pm are also in the//alternate size.final int b = A;while (a) ; 0&& Character.iswhitespace (Format.charat (A-1))) {a--;} format = format.substring (0, a) + magic1+Format.substring (A, B) + "a" + magic2+ format.substring (b + 1);}} Mclockformat = SDF = new SimpleDateFormat (format); mclockformatstring = format;} else {SDF = Mclockformat;} String result = Sdf.format (Mcalendar.gettime ()); if (am_pm_style! = am_pm_style_normal) {int magic1 = Result.indexof ( MAGIC1), int magic2 = Result.indexof (MAGIC2), if (magic1 >= 0 && magic2 > Magic1) {spannablestringbuilder form atted = new Spannablestringbuilder (result), if (Am_pm_style = = Am_pm_style_gone) {formatted.delete (Magic1, Magic2 + 1);} E LSE {if (Am_pm_style = = Am_pm_style_small) {Characterstyle STYLE = new Relativesizespan (0.7f); Formatted.setspan (STYLE, Magic1, magic2,spannable.span_exclusive_inclusive);} Formatted.delete (Magic2, Magic2 + 1); Formatted.delete (Magic1, Magic1 + 1);} return formatted;}} return result;}


Clock display of Android4.4 systemui analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.