@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()); }状态栏秒数还是没有变化,使用getSmallTimeWithSeconds()报错
时间: 2024-04-22 21:28:32 浏览: 96
asp.net实例代码protected override void Render(HtmlTextWriter writer)
这段代码是 Android 平台的 Java 代码,它是一个自定义 View 的实现,用于显示系统时间。在 View 的 onAttachedToWindow() 方法中,它注册了一个 BroadcastReceiver 监听系统时间、时区等变化,并在接收到广播时更新时间的显示。在 updateClock() 方法中,它使用 Calendar 类获取当前时间,并调用 getSmallTime() 方法获取时间的字符串表示,然后将其显示在 View 上。
至于你提到的 getSmallTimeWithSeconds() 方法报错的问题,需要查看具体的错误信息才能确定原因。可以先检查这个方法是否在代码中有定义,如果没有需要先定义。如果已经定义了,可以在调用这个方法时查看具体的错误信息,比如编译错误或运行时异常等。
阅读全文