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 leo
        filter.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 receivers
        IntentFilter 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 leo
        filter.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 日历图标显示日期

时间: 2024-08-05 22:47:34

android launcher 日历图标显示日期的相关文章

android 日历图标显示星期

上一篇文档中谈到了在日历图标上显示当前日期, 现在我添加了几行代码,可以在日历图标上显示对应的星期: 修改源码是在Utilities.java中 函数createCalendarIconBitmap 添加几行代码: int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK); String weekStrings[] = {"周日","周一","周二","周三&quo

Launcher3日历图标跟随日期改变而改变

在上一篇的文章里里实现了动态时钟和动态日历的Launcher图标,但是今天测试的时候发现动态日历会有leakreceived的问题,无法取消Received的绑定.所以今天换一种方式实现动态日历图标: LauncherAppState.java里增加: public static final String ACTION_UPDATE_ICON = "com.xxx.update_icon"; private LauncherAppState() { ---- // UPDATE APP

Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示

参考: http://dev.xiaomi.com/doc/p=3904/index.html http://my.oschina.net/ososchina/blog/352286?p=1#comments https://github.com/leolin310148/ShortcutBadger http://www.voidcn.com/blog/kongbaidepao/article/p-62251.html http://www.eoeandroid.com/thread-5572

Android Launcher 之 图标加框 优化显示效果

直接看代码,解释.主要是用到图片处理的 PorterDuff.Mode.SRC_ATOPSRC_ATOP 源像素和目标像素相混合让上层图居中显示,达到加框效果 package com.ferris.launchericonutils; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas;

android的充电图标显示

最近RK3026的项目需要修改开机充电,才分析了Android原生态的充电过程. 充电的代码和图标在system/core/charger中,会编译成名字为charger的可执行文件,打包进ramdisk中,在init.rc中脚本启动: [java] view plain copy on charger setprop ro.boot.charger.emmc 0 export PATH /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbi

Easyui datebox单击文本框显示日期选择

Easyui默认是点击文本框后面的图标显示日期,为了更进一步优化体验 修改为单击文本框显示日期选择框 修改jquery.easyui.min.js(作者用的是1.3.6版本,其他版本或有区别) 可 ctrl+f 搜索 "_outerWidth():0" 在本行下面添加如下代码: // datebox单击文本框出现日期选择 start if ($(_83f).hasClass("datebox-f")) { _844.click(function() { _845.c

Easyui datebox单击文本框显示日期选择 eayui版本1.5.4.1

Easyui默认是点击文本框后面的图标显示日期,体验很不好,所以我想单击文本框就显示日期选择框,网上很多版本是1.3,1.4的,于是自己就比葫芦画瓢改了一个1.5.4.1的版本. 我参考了网上这个帖子,基本一样.只是1.5.4.1增加了一些代码,所以行号有些差异,并且编码后的数字有些变化.Easyui datebox单击文本框显示日期选择 eayui版本1.5.3 修改jQuery.easyui.min.js文件,在文件中查找:tb._size(opts, 保存,成功. 原文地址:https:/

Android 类似未读短信图标显示数字效果的分析

之前一直以为是应用本身在对图标进行修改,看了源码之后发现其实主要的工作并不是应用自己完成的,主要的工作在是launcher里面完成的. 关于系统里面类似未读短信的具体处理流程如下, 原理 一个应用要实现这个效果,就要在自己有未读的消息的时候发送一个广播告诉系统我有未处理的事件了(例如:短信,电话和邮件等),同时将相关的信息进行保存,比如应用的名称(这里指的是ComponentName)和未处理事件的数量.系统将提升用户有待处理的事件交给Launcher去处理,launcher会接收到对应的广播,

Android自定义日历,可以点击、标注日期、节气、旧历等

1. [图片] 9A59974C-47D4-47E3-8136-3F873EB9BBDC.jpg 2. [图片] left_arrow_pre.png 3. [图片] left_arrow.png 4. [图片] right_arrow_pre.png 5. [图片] right_arrow.png /****************从此出开始将代码拷贝到一个文件中*******************/ package cc.util.android.view; import java.tex