Android13-SystemUI_状态栏-时间多显示秒

172 阅读1分钟

未修改前

%E6%B2%A1%E6%9C%89%E7%A7%92.png

状态栏的布局文件status_bar.xml中的Clock控件

<com.android.systemui.statusbar.policy.Clock
  android:id="@+id/clock"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:textAppearance="@style/TextAppearance.StatusBar.Clock"
  android:singleLine="true"
  android:paddingStart="@dimen/status_bar_left_clock_starting_padding"
  android:paddingEnd="@dimen/status_bar_left_clock_end_padding"
  android:gravity="center_vertical|start"
  />

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java

Clock,继承子TextView,负责时间的显示

public class Clock extends TextView implements
        DemoModeCommandReceiver,
        Tunable,
        CommandQueue.Callbacks,
        DarkReceiver, ConfigurationListener {

Clock提供了显示秒的逻辑

    private boolean mShowSeconds;
    
    private void updateShowSeconds() {
        if (mShowSeconds) {
            // Wait until we have a display to start trying to show seconds.
            if (mSecondsHandler == null && getDisplay() != null) {
                mSecondsHandler = new Handler();
                if (getDisplay().getState() == Display.STATE_ON) {
                    mSecondsHandler.postAtTime(mSecondTick,
                            SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
                }
                mScreenReceiverRegistered = true;
                IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
                filter.addAction(Intent.ACTION_SCREEN_ON);
                mBroadcastDispatcher.registerReceiver(mScreenReceiver, filter);
            }
        } else {
            if (mSecondsHandler != null) {
                mScreenReceiverRegistered = false;
                mBroadcastDispatcher.unregisterReceiver(mScreenReceiver);
                mSecondsHandler.removeCallbacks(mSecondTick);
                mSecondsHandler = null;
                updateClock();
            }
        }
    }
    
    private final Runnable mSecondTick = new Runnable() {
        @Override
        public void run() {
            if (mCalendar != null) {
                updateClock();
            }
            mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000);
        }
    };
    //设置时间文本值
    final void updateClock() {
        if (mDemoMode) return;
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        CharSequence smallTime = getSmallTime();
        // Setting text actually triggers a layout pass (because the text view is set to
        // wrap_content width and TextView always relayouts for this). Avoid needless
        // relayout if the text didn't actually change.
        if (!TextUtils.equals(smallTime, getText())) {
            setText(smallTime);
        }
        setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime()));
    }

在updateShowSeconds()方法前把mShowSeconds职位true

    @Override
    public void onTuningChanged(String key, String newValue) {
        if (CLOCK_SECONDS.equals(key)) {
            mShowSeconds = TunerService.parseIntegerSwitch(newValue, false);
            //原有逻辑就有显示秒的逻辑,在更新前置为true
            mShowSeconds = true;
            updateShowSeconds();
        } else if (StatusBarIconController.ICON_HIDE_LIST.equals(key)) {
            setClockVisibleByUser(!StatusBarIconController.getIconHideList(getContext(), newValue)
                    .contains("clock"));
            updateClockVisibility();
        }
    }

效果

%E6%98%BE%E7%A4%BA%E7%A7%92%E6%95%88%E6%9E%9C.gif