AlexTextClock.java 4.23 KB
package com.sw.laryngoscope.widget;

/**
 * @Description 自定义字体,细,缺口棱角分明
 * @Author
 * @Time 2019/01/18
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.sw.laryngoscope.utils.Logger;

import java.util.Calendar;

public class AlexTextClock extends AppCompatTextView {
    //private String timeFormat = "yyyy年MM月dd日 hh:mm:ss EEEE";String delegate = "hh:mm aaa";
    private String time12Format = "hh:mm";//"HH:mm:ss";
    private String time24Format = "HH:mm";//"hh:mm:ss";

    private Calendar mTime;

    private static boolean isReg = false;

    public AlexTextClock(@NonNull Context context) {
        super(context);
    }

    public AlexTextClock(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AlexTextClock(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            onTimeChanged();
            Logger.d("-----------------.----111  " + getText());
        }
    };

    private void onTimeChanged() {
        //ContentResolver mResolver= AlexTextClock.this.getContentResolver();
        //String timeFormat = android.provider.Settings.System.getString(mResolver,android.provider.Settings.System.TIME_12_24);
        //---mTime.setTimeInMillis(System.currentTimeMillis());
        Calendar mCalendar = Calendar.getInstance();
        //if ( mCalendar.get(Calendar.AM_PM )==0 ) {
        /*if ( TimerChangeManager.getInstance().isShowSet() ) {
            setText(TimerChangeManager.getInstance().getTimeStr());
        } else {*/
            if ( DateFormat.is24HourFormat(getContext()) ) {
                setText(DateFormat.format(time24Format, mCalendar.getTimeInMillis()));
            } else {
                setText(DateFormat.format(time12Format, mCalendar.getTimeInMillis()));
            }
        //}

        //Logger.d("---------------------111  " + getText());
    }

    private final Runnable mTicker = new Runnable() {
        public void run() {

            onTimeChanged();

            long now = SystemClock.uptimeMillis();
            long next = now + (1000 - now % 1000);

            mMsgHandler.postAtTime(mTicker, next);
            //Logger.d("-----------------0----111  " + getText());
        }
    };

    private Handler mMsgHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            }
        }
    };

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mTime = Calendar.getInstance();
        mTime.setTimeInMillis(System.currentTimeMillis());
        //setText(DateFormat.format(timeFormat, mTime));
        if ( DateFormat.is24HourFormat(getContext()) ) {
            setText(DateFormat.format(time12Format, mTime));
        } else {
            setText(DateFormat.format(time24Format, mTime));
        }
        mTicker.run();

        final IntentFilter filter = new IntentFilter();

        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        Logger.d("---------------------111");
        //if ( !isReg ) {
            getContext().registerReceiver(mIntentReceiver, filter);
        //}
        isReg = true;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Logger.d("---------------------222");
        //if ( !isReg ) {
            getContext().unregisterReceiver(mIntentReceiver);
        //}
        //isReg = false;
    }
}