@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (!mAttached) { mAttached = true; IntentFilter 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 do these after registering the receiver since the receiver always runs // in the main thread, therefore the receiver can't run before this method returns. // The time zone may have changed while the receiver wasn't registered, so update the Time mCalendar = Calendar.getInstance(TimeZone.getDefault()); // Make sure we update to the current time postDelayed(new Runnable() { @Override public void run() { updateClock(); } }, 1000); } private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); 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()); } } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { final Locale newLocale = getResources().getConfiguration().locale; if (! newLocale.equals(mLocale)) { mLocale = newLocale; mClockFormatString = ""; // force refresh } } updateClock(); } }; final void updateClock() { if (mDemoMode) return; mCalendar.setTimeInMillis(System.currentTimeMillis()); setText(getSmallTime()); }状态栏秒数还是没有变化
时间: 2024-04-22 13:28:46 浏览: 72
asp.net实例代码protected override void Render(HtmlTextWriter writer)
这段代码可以实现状态栏中的时间实时更新,但是它只更新了小时和分钟,没有更新秒数。在 `updateClock()` 方法中调用了 `getSmallTime()` 方法来获取小时和分钟的显示字符串,但是该方法并没有包含秒数的信息。如果您希望在状态栏中显示秒数,需要修改 `getSmallTime()` 方法,使其包含秒数的信息。或者您也可以使用 `getSmallTimeWithSeconds()` 方法来获取包含秒数信息的显示字符串。
阅读全文