日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 监听时钟变化,Android4.4 SystemUI分析之Clock时钟显示

發布時間:2025/3/21 Android 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 监听时钟变化,Android4.4 SystemUI分析之Clock时钟显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SystemUI上的時間顯示只要就在/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java類上

效果圖

這個類也很簡單,監聽處理廣播

@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

updateClock();

}

@Override

protected void onDetachedFromWindow() {

super.onDetachedFromWindow();

if (mAttached) {

getContext().unregisterReceiver(mIntentReceiver);

mAttached = false;

}

}在廣播中去更新時間和監聽語言是否有改變

private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

Log.d(TAG, "action =" + action);

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());

}

Log.d(TAG,

"mCalendar =" + mCalendar

+ "TimeZone.getTimeZone(tz) ="

+ TimeZone.getTimeZone(tz));

} else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {

final Locale newLocale = getResources().getConfiguration().locale;

if (!newLocale.equals(mLocale)) {

mLocale = newLocale;

mClockFormatString = ""; // force refresh

}

}

updateClock();

}

};在設置中可以改變時間顯示是以12小時還是24小時

時間的格式合成都是在這個函數中完成

private final CharSequence getSmallTime() {

Context context = getContext();

boolean is24 = DateFormat.is24HourFormat(context);

LocaleData d = LocaleData

.get(context.getResources().getConfiguration().locale);

final char MAGIC1 = '\uEF00';

final char MAGIC2 = '\uEF01';

SimpleDateFormat sdf;

String format = is24 ? d.timeFormat24 : d.timeFormat12;

if (!format.equals(mClockFormatString)) {

/*

* Search for an unquoted "a" in the format string, so we can add

* dummy characters around it to let us find it again after

* formatting and change its size.

*/

if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {

int a = -1;

boolean quoted = false;

for (int i = 0; i < format.length(); i++) {

char c = format.charAt(i);

if (c == '\'') {

quoted = !quoted;

}

if (!quoted && c == 'a') {

a = i;

break;

}

}

if (a >= 0) {

// Move a back so any whitespace before AM/PM is also in the

// alternate size.

final int b = a;

while (a > 0

&& Character.isWhitespace(format.charAt(a - 1))) {

a--;

}

format = format.substring(0, a) + MAGIC1

+ format.substring(a, b) + "a" + MAGIC2

+ format.substring(b + 1);

}

}

mClockFormat = sdf = new SimpleDateFormat(format);

mClockFormatString = format;

} else {

sdf = mClockFormat;

}

String result = sdf.format(mCalendar.getTime());

if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {

int magic1 = result.indexOf(MAGIC1);

int magic2 = result.indexOf(MAGIC2);

if (magic1 >= 0 && magic2 > magic1) {

SpannableStringBuilder formatted = new SpannableStringBuilder(

result);

if (AM_PM_STYLE == AM_PM_STYLE_GONE) {

formatted.delete(magic1, magic2 + 1);

} else {

if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {

CharacterStyle style = new RelativeSizeSpan(0.7f);

formatted.setSpan(style, magic1, magic2,

Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

}

formatted.delete(magic2, magic2 + 1);

formatted.delete(magic1, magic1 + 1);

}

return formatted;

}

}

return result;

}

總結

以上是生活随笔為你收集整理的android 监听时钟变化,Android4.4 SystemUI分析之Clock时钟显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。