由于角标在Android原生的系统中没有支持,所以各个Android手机厂商各自为政。
正如很多资料所说,这一功能完全是效仿IOS,Android本不存在的,对于不支持的厂商如魅族、中兴、酷派,必须为他们不盲目跟风而点赞。
做起适配来,真的很麻烦,要针对各个厂商逐个去写以及测试。
目前做到支持小米、华为、三星、LG、VIVO、ZUK、HTC、NOVA等厂商的Andorid系统
效果如下图所示
特别说明一下小米自MIUI6.0以后,角标的显示和通知栏Notification绑在一起。目前我发现的做法,就只有小米需要和Notification绑在一起才能生效。
不过我在这里的例子因产品需求,全部机型的做法都把Notification绑上。
消息通知ID
private int MQTT_IM_NOTIFICATION_ID=1007;
通知栏Notification的定义
String content = 2 + "个联系人发了" + i + "条消息";
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
builder.setSmallIcon(R.drawable.chat_notify_icon);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setTicker("收到" + i + "条消息");
builder.setWhen(System.currentTimeMillis());
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.setAction(getApplicationContext().getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
builder.setContentTitle(getResources().getText(R.string.app_name));
builder.setContentText(content);
final Notification n = builder.build();
int defaults = Notification.DEFAULT_LIGHTS;
defaults |= Notification.DEFAULT_SOUND;
defaults |= Notification.DEFAULT_VIBRATE;
n.defaults = defaults;
n.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
厂商
private static String SYSTEM_XIAOMI="XIAOMI";
private static String SYSTEM_SAMSUNG="SAMSUNG";
private static String SYSTEM_HUAWEI_HONOR="HONOR";
private static String SYSTEM_HUAWEI="HUAWEI";
private static String SYSTEM_NOVA="NOVA";
private static String SYSTEM_SONY="SONY";
private static String SYSTEM_VIVO="VIVO";
private static String SYSTEM_OPPO="OPPO";
private static String SYSTEM_LG="LG";
private static String SYSTEM_ZUK="ZUK";
private static String SYSTEM_HTC="HTC";
厂商获取
OSName=android.os.Build.BRAND.trim().toUpperCase();
类名的获取
主要是得到Activity名字,得到完整的包名、类名
//获取类名
public static String getLauncherClassName(Context context) {
PackageManager packageManager = context.getPackageManager();