開始
大家經(jīng)常看見在小米或者三星系統(tǒng)上八回,應(yīng)用會把應(yīng)用的消息數(shù)量想蘋果手機(jī)一樣顯示在右上角酷愧,其實(shí)這種消息提示的方法在原生Android上是不支持的,原生的launcher在圖標(biāo)上沒有顯示徽標(biāo)的view缠诅。
但是小米溶浴、三星、華為管引、聯(lián)想士败、索尼等手機(jī)廠商都沒有使用原生的launcher,他們使用的桌面都是自己重寫的褥伴。所以他們都已經(jīng)實(shí)現(xiàn)了在桌面圖標(biāo)上顯示消息數(shù)量徽標(biāo)的方法谅将,我們只要進(jìn)行調(diào)用就行了。
先上圖(設(shè)備有限重慢,只有小米和三星):
小米實(shí)現(xiàn)的源碼:
/**
* 在小米手機(jī)上顯示桌面徽標(biāo)
*
* @param context
* @param num
*/
private static void xiaoMiShortCut(Context context, int num) {
boolean isMiUIV6 = true;
try {
Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
Object miuiNotification = miuiNotificationClass.newInstance();
Field field = miuiNotification.getClass().getDeclaredField("messageCount");
field.setAccessible(true);
field.set(miuiNotification, num);// 設(shè)置信息數(shù)
field = notification.getClass().getField("extraNotification");
field.setAccessible(true);
field.set(notification, miuiNotification);
} catch (Exception e) {
e.printStackTrace();
//miui 6之前的版本
isMiUIV6 = false;
Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
localIntent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/.login.WelcomeActivity");
localIntent.putExtra("android.intent.extra.update_application_message_text", num);
context.sendBroadcast(localIntent);
} finally {
if (notification != null && isMiUIV6) {
//miui6以上版本需要使用通知發(fā)送
nm.notify(10201, notification);
}
}
}
三星上實(shí)現(xiàn)的源碼:
/**
* 在三星手機(jī)上顯示桌面徽標(biāo)
*
* @param context 上下文
* @param num 顯示的消息數(shù)量饥臂,整數(shù)
*/
private static void samsungShortCut(Context context, int num) {
String launcherClassName = getLaunchActivityName(context);
if (launcherClassName == null) {
return;
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", num);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
// Toast.makeText(context, "三星手機(jī)," + "廣播已經(jīng)發(fā)送", Toast.LENGTH_LONG).show();
}
關(guān)于MIUI的一點(diǎn)看法
1.大家應(yīng)該已經(jīng)看出,小米在MIUI6以后和三星的實(shí)現(xiàn)方式不一樣了似踱,小米將系統(tǒng)的Notification改寫成了自己的MiuiNotification隅熙,增加了messageCount字段,用于存儲消息數(shù)量核芽,通過反射將消息數(shù)量設(shè)置到了messageCount里囚戚。此處的num一定要是整數(shù)~
2.MIUI6以下,通過發(fā)送廣播即可完成徽標(biāo)設(shè)置~
3.通過上面可以發(fā)現(xiàn)狞洋,MIUI6以上系統(tǒng)弯淘,在顯示桌面徽標(biāo)數(shù)量的時候是通過統(tǒng)計(jì)在系統(tǒng)通知欄中的所有該應(yīng)用的notification的messageCount的總和,如果你清除了那條通知吉懊,那么桌面徽標(biāo)也就跟隨去掉庐橙。
4.我個人覺得這樣的實(shí)現(xiàn)方式比較好假勿,也方便進(jìn)行管理,畢竟顯示的徽標(biāo)也是一種notification态鳖,那么就應(yīng)該和系統(tǒng)通知同步進(jìn)行管理转培。
關(guān)于三星的一點(diǎn)看法
1.在三星的實(shí)現(xiàn)方式上,系統(tǒng)通知和桌面徽標(biāo)數(shù)量是沒有聯(lián)系的浆竭,三星的桌面應(yīng)用是TouchWiz.apk浸须,通過反編譯可以看到,在launcher類中定義了action為“android.intent.action.BADGE_COUNT_UPDATE”的廣播接收者邦泄,我之前在網(wǎng)上查找到的資料删窒,在設(shè)置消息條數(shù)的時候設(shè)置的都是String類型的,導(dǎo)致我一直不能顯示徽標(biāo)顺囊,人家廣播接收者里面只接受int類型的肌索。。特碳。
2.三星在設(shè)置完徽標(biāo)數(shù)量后诚亚,是將徽標(biāo)數(shù)量持久化了的,也就是如果你需要去掉徽標(biāo)午乓,得重新發(fā)送廣播站宗,將數(shù)量設(shè)置為0即可。個人覺得沒有小米的管理方便~
關(guān)于其他類型手機(jī)的徽標(biāo)設(shè)置
通過反編譯麻花騰的產(chǎn)品看到益愈,他還判斷聯(lián)想梢灭、華為、索尼等產(chǎn)品腕唧,附上一些代碼(下面代碼除華為外或辖,其他沒有進(jìn)行測試#華為的測試結(jié)果是跑不通):
/**
* 創(chuàng)建索尼桌面徽標(biāo)
*
* @param context
* @param num
*/
private static void setSonyBadge(Context context, int num) {
Intent localIntent = new Intent();
String str1 = getLaunchActivityName(context);
if (str1 == null)
return;
boolean isShow = false;
if (num < 1) {
isShow = false;
} else if (num > 99) {
isShow = true;
}
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);
localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", str1);
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "");
localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
context.sendBroadcast(localIntent);
}
/**
* 判斷是否是聯(lián)想手機(jī)
*
* @param context
* @param paramString
* @return
*/
private static boolean islenovoLanucher(Context context, String paramString) {
try {
if (null == packmag)
packmag = context.getPackageManager();
float f = Float.valueOf(Float.parseFloat(packmag.getPackageInfo(paramString, 0).versionName.substring(0, 3))).floatValue();
if (f >= 6.7F)
return true;
} catch (Exception localException) {
return false;
}
return false;
}
華為可以參見華為桌面未讀角標(biāo)
以上內(nèi)容僅個人觀點(diǎn),能力有限枣接,如有不足,歡迎指導(dǎo)缺谴,勿噴~謝謝