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

相关文章

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

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