给大家分享一个实现android系统上实现桌面icon未读信息气泡效果。类似于iPhone的badge。是从github上找到的源代码。从原文上来看目前只支持samsung和LG,三星亲测有效。


import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;//BadgeUtil provides static utility methods to set "badge count" on Launcher (by Samsung, LG). //Currently, it's working from Android 4.0. //But some devices, which are released from the manufacturers, are not working.public class BadgeUtil {    private static final String TAG = "BadgeUtil";    private static final String ACTION_BADGE_COUNT_UPDATE = "android.intent.action.BADGE_COUNT_UPDATE";    private static final String EXTRA_BADGE_COUNT = "badge_count";    private static final String EXTRA_BADGE_COUNT_PACKAGE_NAME = "badge_count_package_name";    private static final String EXTRA_BADGE_COUNT_CLASS_NAME = "badge_count_class_name";        public static int badgeCount = 0;    /**     * Set badge count     *      * @param context The context of the application package.     * @param count Badge count to be set     */    public static void setBadgeCount(Context context, int count) {        Intent badgeIntent = new Intent(ACTION_BADGE_COUNT_UPDATE);        badgeIntent.putExtra(EXTRA_BADGE_COUNT, badgeCount);        badgeIntent.putExtra(EXTRA_BADGE_COUNT_PACKAGE_NAME, context.getPackageName());        badgeIntent.putExtra(EXTRA_BADGE_COUNT_CLASS_NAME, getLauncherClassName(context));        context.sendBroadcast(badgeIntent);    }    /**     * Reset badge count. The badge count is set to "0"     *      * @param context The context of the application package.     */    public static void resetBadgeCount(Context context) {    badgeCount = 0;        setBadgeCount(context, 0);    }    /**     * Retrieve launcher activity name of the application from the context     *     * @param context The context of the application package.     * @return launcher activity name of this application. From the     *         "android:name" attribute.     */    private static String getLauncherClassName(Context context) {        PackageManager packageManager = context.getPackageManager();        Intent intent = new Intent(Intent.ACTION_MAIN);        // To limit the components this Intent will resolve to, by setting an        // explicit package name.        intent.setPackage(context.getPackageName());        intent.addCategory(Intent.CATEGORY_LAUNCHER);        // All Application must have 1 Activity at least.        // Launcher activity must be found!        ResolveInfo info = packageManager                .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);        // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER        // if there is no Activity which has filtered by CATEGORY_DEFAULT        if (info == null) {            info = packageManager.resolveActivity(intent, 0);        }        return info.activityInfo.name;    }}


下面是截图



原网址:https://github.com/ekinlyw/android-badge

希望大家学习的时候也能感谢原作者的辛苦劳动




更多相关文章

  1. 异曲同工 7个Android应用程序源代码打包下载(与10个不一样)
  2. window 下 利用gradle编译volley源代码
  3. 如何将当前布局用代码保存在png图像文件中?
  4. Android(安卓)向服务器发送get请求乱码问题
  5. ejoy2d源代码阅读之一(启动流程)
  6. ubuntu12.04(64bit)编译android-3.4内核代码
  7. Android(安卓)自定义View双向选择带气泡SeekBar
  8. Android手动绘制ninepath(.9.png)图片
  9. 安卓截图笔记

随机推荐

  1. 为什么要用内插字符串代替string.format
  2. 8天入门docker系列 —— 第三天 使用aspn
  3. 想要成为一名优秀的软件测试工程师,这8项
  4. 8天入门docker系列 —— 第四天 使用aspn
  5. 从GC的SuppressFinalize方法带你深刻认识
  6. 当模板方法遇到了委托函数,你的代码又可以
  7. Linq中带有迭代索引的Select扩展方法,为啥
  8. 面向接口编程,你考虑过性能吗?
  9. 慎用ToLower和ToUpper,小心把你的系统给拖
  10. 同步异步多线程这三者关系,你能给面试官一