AlexTextClock.java
4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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;
}
}