Android App的ICON上显示红色数字
来源:互联网 发布:法学入门书籍知乎 编辑:程序博客网 时间:2024/05/01 07:52
import android.app.Notification;import android.app.NotificationManager;import android.content.ComponentName;import android.content.ContentValues;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.net.Uri;import android.os.Build;import android.support.v7.app.NotificationCompat;import android.widget.Toast;import java.lang.reflect.Field;import java.lang.reflect.Method;public final class BadgeUtil { private BadgeUtil() throws InstantiationException { throw new InstantiationException("This class is not for instantiation"); } /** * 设置Badge 目前支持Launcher */ public static void setBadgeCount(Context context, int count, int iconResId) { if (count <= 0) { count = 0; } else { count = Math.max(0, Math.min(count, 99)); } if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) { setBadgeOfMIUI(context, count, iconResId); } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) { setBadgeOfSony(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("samsung") || Build.MANUFACTURER.toLowerCase().contains("lg")) { setBadgeOfSumsung(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("htc")) { setBadgeOfHTC(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("nova")) { setBadgeOfNova(context, count); } else { Toast.makeText(context, "Not Found Support Launcher", Toast.LENGTH_LONG).show(); } } /** * 设置MIUI的Badge */ private static void setBadgeOfMIUI(Context context, int count, int iconResId) { NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("标题").setContentText("消息正文").setSmallIcon(iconResId); Notification notification = builder.build(); try { Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, count); } catch (Exception e) { e.printStackTrace(); } mNotificationManager.notify(0, notification); } /** * 设置索尼的Badge * 需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> */ private static void setBadgeOfSony(Context context, int count) { String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } boolean isShow = true; if (count == 0) { isShow = false; } Intent localIntent = new Intent(); localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE"); localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);//是否显示 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);//启动页 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));//数字 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名 context.sendBroadcast(localIntent); } /** * 设置三星的Badge\设置LG的Badge */ private static void setBadgeOfSumsung(Context context, int count) { // 获取你当前的应用 String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", count); intent.putExtra("badge_count_package_name", context.getPackageName()); intent.putExtra("badge_count_class_name", launcherClassName); context.sendBroadcast(intent); } /** * 设置HTC的Badge */ private static void setBadgeOfHTC(Context context, int count) { Intent intentNotification = new Intent("com.htc.launcher.action.SET_NOTIFICATION"); ComponentName localComponentName = new ComponentName(context.getPackageName(), getLauncherClassName(context)); intentNotification.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString()); intentNotification.putExtra("com.htc.launcher.extra.COUNT", count); context.sendBroadcast(intentNotification); Intent intentShortcut = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT"); intentShortcut.putExtra("packagename", context.getPackageName()); intentShortcut.putExtra("count", count); context.sendBroadcast(intentShortcut); } /** * 设置Nova的Badge */ private static void setBadgeOfNova(Context context, int count) { ContentValues contentValues = new ContentValues(); contentValues.put("tag", context.getPackageName() + "/" + getLauncherClassName(context)); contentValues.put("count", count); context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"), contentValues); } public static void setBadgeOfMadMode(Context context, int count, String packageName, String className) { Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", count); intent.putExtra("badge_count_package_name", packageName); intent.putExtra("badge_count_class_name", className); context.sendBroadcast(intent); } /** * 重置Badge */ public static void resetBadgeCount(Context context, int iconResId) { setBadgeCount(context, 0, iconResId); } public static String getLauncherClassName(Context context) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.setPackage(context.getPackageName()); intent.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (info == null) { info = packageManager.resolveActivity(intent, 0); } return info.activityInfo.name; }
BadgeUtil.setBadgeCount(getApplicationContext(), 10, R.mipmap.ic_launcher);
0 0
- Android App的ICON上显示红色数字
- app Icon上显示数字
- Android ActionBar上不显示icon的问题
- Android ActionBar上不显示icon的问题
- 关于Android ActionBar上不显示icon的问题
- 如何实现ICON右上角的数字显示
- 设置ICON图标上的数字
- App icon 显示版本号
- android studio中app显示红色,无法编译
- Android通过输入暗码控制指定app icon的显示与隐藏
- 怎么xxx.app一直都是红色的? 编译了很多次 显示成功,但就是红色
- Android图标上显示数字
- Android 图标上显示数字
- 如何去掉app icon上的玻璃效果
- 开发Ios、Android程序快捷方式上显示的提示数字
- Launcher hotseat上icon显示文字的修改
- Android隐藏Icon,条件启动显示icon
- android工程显示红色叹号,Android Dependencies显示红色×
- 第9章:进一步的Type
- java api名人的故事(一):时间api的那些事
- PHP SOCKET编程
- mysql 查询语法记录
- Android中常见的内存泄漏
- Android App的ICON上显示红色数字
- J2EE面试题(二)
- 二叉树
- SVD的物理意义
- 字符串转浮点型数据C语言实现
- CentOS 6 /ubuntu配置 PPTP VPN 概述
- SurvivalShooter学习笔记(六.玩家生命)
- J2EE面试题(三)
- 雷军:我十年的程序员生涯