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

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

macOS怎么轻松更换App图标? Mac电脑图标更换指南

《macOS怎么轻松更换App图标?Mac电脑图标更换指南》想要给你的Mac电脑按照自己的喜好来更换App图标?其实非常简单,只需要两步就能搞定,下面我来详细讲解一下... 虽然 MACOS 的个性化定制选项已经「缩水」,不如早期版本那么丰富,www.chinasem.cn但我们仍然可以按照自己的喜好来更换

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超