android launcher 日历图标显示日期

2024-03-26 19:08

本文主要是介绍android launcher 日历图标显示日期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

看到iphone上的日历图标上的数字会随着日期的变化而变化,最近在android平台上也研究了 一下,实现方法如下:

直接上源码

在launcher里改的

首先,在IconCache.java文件中,找到方法private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
            HashMap<Object, CharSequence> labelCache)
在entry.icon = Utilities.createIconBitmap(icon, mContext); 这个位置修改:

<span style="font-size:14px;">if(info.activityInfo.packageName.equals("com.android.calendar")){entry.icon = Utilities.createCalendarIconBitmap(icon, mContext);}else{if (index >= 0) {entry.icon = Utilities.createIconBitmap(icon, mContext);} else {entry.icon = Utilities.createIconBitmap(/* SPRD: Feature 253522, Remove the application drawer view @{ */// getFullResIcon(info), mContext);icon, mContext, true);}}</span>


改后源码如下:

<span style="font-size:14px;">private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,HashMap<Object, CharSequence> labelCache) {CacheEntry entry = mCache.get(componentName);if (entry == null) {entry = new CacheEntry();mCache.put(componentName, entry);ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);if (labelCache != null && labelCache.containsKey(key)) {entry.title = labelCache.get(key).toString();} else {entry.title = info.loadLabel(mPackageManager).toString();if (labelCache != null) {labelCache.put(key, entry.title);}}if (entry.title == null) {entry.title = info.activityInfo.name;}/* SPRD: Fix bug 281291, remove icon_top for theme defaut icon @{ *//* SPRD: UUI theme : system icons @{ */Drawable icon;/* SPRD: Fix bug294355, add just to ThemeManager. @{ */int index = sysIndexOf(componentName.getClassName());Log.i("jxt", "index:"+index+",Name:"+componentName.getClassName());icon = getFullResIcon(info);//changed by  leo if(info.activityInfo.packageName.equals("com.android.calendar")){entry.icon = Utilities.createCalendarIconBitmap(icon, mContext);}else{if (index >= 0) {entry.icon = Utilities.createIconBitmap(icon, mContext);} else {entry.icon = Utilities.createIconBitmap(/* SPRD: Feature 253522, Remove the application drawer view @{ */// getFullResIcon(info), mContext);icon, mContext, true);}}/* @} *//* @} *//* @} */}return entry;}</span>

接下来修改Utilities.java

添加一个函数:


<span style="font-size:14px;">static Bitmap createCalendarIconBitmap(Drawable icon, Context context){Bitmap calendarIcon = createIconBitmap(icon,context);String dayString  = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));synchronized (sCanvas) {final Canvas canvas = sCanvas;canvas.setBitmap(calendarIcon);final float mDensity = context.getResources().getDisplayMetrics().density;Paint mDatePaint = new Paint();mDatePaint.setTypeface(Typeface.DEFAULT_BOLD);mDatePaint.setTextSize((int)30F * mDensity);mDatePaint.setColor(0xff000000);mDatePaint.setAntiAlias(true);Rect rect = new Rect();mDatePaint.getTextBounds(dayString,0,dayString.length(),rect);int hoffset = 20;int width1 = rect.right - rect.left;int height1 = rect.bottom - rect.top;int width2 = calendarIcon.getWidth();int height2 = calendarIcon.getHeight() + hoffset;canvas.drawText(dayString,(width2 - width1)/2 - rect.left,(height2 - height1)/2 - rect.top,mDatePaint);canvas.setBitmap(null);return calendarIcon;}}</span>
再修改LauncherModel.java文件:

在onReceive()方法中添加如下代码:

<span style="font-size:14px;">//changed by leo}else if(Intent.ACTION_DATE_CHANGED.equals(action) ||Intent.ACTION_TIME_CHANGED.equals(action) ||Intent.ACTION_TIMEZONE_CHANGED.equals(action)){final String packageName = "com.android.calendar";enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, new String[]{packageName}));}</span>
最后修改LauncherApplication.java文件,如果是launcher3的源码,则修改LauncherAppState.java文件

我这里修改的是

LauncherAppState.java

在构造函数 private LauncherAppState()中添加:

<span style="font-size:14px;"> //changed by leofilter.addAction(Intent.ACTION_TIME_CHANGED);filter.addAction(Intent.ACTION_DATE_CHANGED);filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);</span>

修改后的样式为:

<span style="font-size:14px;"> // Register intent receiversIntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);filter.addAction(Intent.ACTION_PACKAGE_REMOVED);filter.addAction(Intent.ACTION_PACKAGE_CHANGED);filter.addDataScheme("package");sContext.registerReceiver(mModel, filter);filter = new IntentFilter();filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);filter.addAction(Intent.ACTION_LOCALE_CHANGED);filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);//changed by leofilter.addAction(Intent.ACTION_TIME_CHANGED);filter.addAction(Intent.ACTION_DATE_CHANGED);filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);sContext.registerReceiver(mModel, filter);
</span>
当然, 这样是不能运行看到效果的,要将该导入的包都导入 ,然后再单编译launcher模块,push到手机里,就能看到日历图标上显示当前日期了。

      今天看到好多人修改后引起了问题,我把源码提取出来了,放在资源里,需要的可以去下载查看,请点击链接


这篇关于android launcher 日历图标显示日期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/849551

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Java日期类详解(最新推荐)

《Java日期类详解(最新推荐)》早期版本主要使用java.util.Date、java.util.Calendar等类,Java8及以后引入了新的日期和时间API(JSR310),包含在ja... 目录旧的日期时间API新的日期时间 API(Java 8+)获取时间戳时间计算与其他日期时间类型的转换Dur

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio