Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字
背景:
在Android原生系統(tǒng)中,眾所周知不支持桌面圖標(biāo)顯示未讀消息提醒的數(shù)字,雖然第三方控件BadgeView可以實(shí)現(xiàn)應(yīng)用內(nèi)的數(shù)字提醒。但對于系統(tǒng)的圖標(biāo),特別是app的logo圖標(biāo)很難實(shí)現(xiàn)數(shù)字標(biāo)志,即使是繪圖的方式不斷修改,但這種方式天生弊端,實(shí)用性很差。但幸運(yùn)的是,一些強(qiáng)大的手機(jī)廠商(小米,三星,索尼)提供了私有的API,但也帶來了難度,API的不同就意味著代碼量的增加和兼容性問題更加突出。
現(xiàn)在我們來看看他們是如何實(shí)現(xiàn)的:
實(shí)現(xiàn)原理:
首先我們要明白 并不是應(yīng)用本身處理對啟動圖標(biāo)進(jìn)行修改、圖標(biāo)的動態(tài)修改的過程主要是在Launcher里面完成的.在應(yīng)用安裝,更新,卸載的時(shí)候,都會有廣播發(fā)出,Launcher在LauncherApplication 中注冊廣播,在LauncherModel中處理接收到廣播的消息,重新加載更新應(yīng)用信息(如:應(yīng)用圖標(biāo)、文字等)。但是原生的android系統(tǒng)是并不支持該特性的(及不能通過發(fā)送特定的系統(tǒng)廣播 達(dá)到動態(tài)修改啟動圖標(biāo)的效果),但是在強(qiáng)大的第三方Android手機(jī)廠商(如:三星、小米)的系統(tǒng)源碼深度定制下、通過修改了Launcher源代碼,增加/注冊了新的廣播接收器用來接收應(yīng)用發(fā)送來的未讀消息數(shù)廣播,接收到廣播后,系統(tǒng)將未讀消息的數(shù)目顯示事件交給Launcher去處理,調(diào)用相關(guān)方法去重繪應(yīng)用的icon,最終達(dá)到動態(tài)更新應(yīng)用圖標(biāo)的效果。
示例代碼:
public class LauncherBadgeHelper { /** * Set badge count * 針對 Samsung / xiaomi / sony 手機(jī)有效 * * @param context The context of the application package. * @param count Badge count to be set */ public static void setBadgeCount(Context context, int count) { if (count <= 0) { count = 0; } else { count = Math.max(0, Math.min(count, 99)); } if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) { sendToXiaoMi(context, count); } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) { sendToSony(context, count); } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) { sendToSamsumg(context, count); } else { sendToSamsumg(context, count); } } /** * 向小米手機(jī)發(fā)送未讀消息數(shù)廣播 * * @param count */ private static void sendToXiaoMi(Context context, int count) { try { Class miuiNotificationClass = Class.forName("android.app.MiuiNotification"); Object miuiNotification = miuiNotificationClass.newInstance(); Field field = miuiNotification.getClass().getDeclaredField("messageCount"); field.setAccessible(true); field.set(miuiNotification, String.valueOf(count == 0 ? "" : count)); // 設(shè)置信息數(shù)-->這種發(fā)送必須是miui 6才行 } catch (Exception e) { LogController.e(e.toString()); // miui 6之前的版本 Intent localIntent = new Intent( "android.intent.action.APPLICATION_MESSAGE_UPDATE"); localIntent.putExtra( "android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context)); localIntent.putExtra( "android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count)); context.sendBroadcast(localIntent); } } /** * 向索尼手機(jī)發(fā)送未讀消息數(shù)廣播 * 據(jù)說:需添加權(quán)限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"> [未驗(yàn)證] * * @param count */ private static void sendToSony(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));//數(shù)字 localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名 context.sendBroadcast(localIntent); } /** * 向三星手機(jī)發(fā)送未讀消息數(shù)廣播 * * @param count */ private static void sendToSamsumg(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); } /** * 重置、清除Badge未讀顯示數(shù) * * @param context */ public static void resetBadgeCount(Context context) { 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; } }</uses-permission>
可以看出小米,三星,索尼處理方式都是通過發(fā)送廣播來實(shí)現(xiàn)的。
但是:小米MIUI6以后,改變了處理方式,棄用了發(fā)送廣播的方式,改為通過發(fā)送通知。
一、基本介紹
1、默認(rèn)的情況
當(dāng)app 向通知欄發(fā)送了一條通知 (通知不帶進(jìn)度條并且用戶可以刪除的),那么桌面app icon角標(biāo)就會顯示1.此時(shí)app顯示的角標(biāo)數(shù)是和通知欄里app發(fā)送的通知數(shù)對應(yīng)的,即向通知欄發(fā)送了多少通知就會顯示多少角標(biāo)
二、實(shí)現(xiàn)代碼
第三方app需要用反射來調(diào)用,參考代碼:
NotificationManager mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(this) .setContentTitle(“title”).setContentText(“text”).setSmallIcon(R.drawable.icon); 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, mCount); } catch (Exception e) { e.printStackTrace(); } mNotificationManager.notify(0,notification);
自己在之前的代碼根據(jù)官方代碼總結(jié)新的方法如下:
/** * 向小米手機(jī)發(fā)送未讀消息數(shù)廣播miui6以后 * * @param count */ private static void sendToXiaoMi2(Context context, int count) { NotificationManager mNotificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(MyApplication.getContext()).setContentTitle("title").setContentText("text").setSmallIcon(R.drawable.ico_haoyilogo); Notification notification = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 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); mNotificationManager.notify(10, notification); } catch (Exception e) { e.printStackTrace(); LogController.e(e.toString()); // miui 6之前的版本 Intent localIntent = new Intent( "android.intent.action.APPLICATION_MESSAGE_UPDATE"); localIntent.putExtra( "android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherClassName(context)); localIntent.putExtra( "android.intent.extra.update_application_message_text", String.valueOf(count == 0 ? "" : count)); context.sendBroadcast(localIntent); } }
這樣既能兼容MIUI6之前的,還能實(shí)現(xiàn)MIUI6以后的。自己在開發(fā)的時(shí)候經(jīng)過測試,發(fā)現(xiàn)MIUI內(nèi)部對于相同的消息數(shù)字是不顯示的,由于我測試的時(shí)候用的是寫死的數(shù)字,導(dǎo)致走了很多彎路。還有,自己在查找資料的時(shí)候發(fā)現(xiàn)有許多朋友都遇到過這樣的問題,未讀消息數(shù)字只有在第一次安裝的時(shí)候才顯示,進(jìn)入后再設(shè)置就沒有了,我估計(jì)都是因?yàn)閿?shù)字相同造成的。
細(xì)想一下,MIUI這種做法也挺好的,消息數(shù)字和通知綁定,當(dāng)來通知時(shí)觸發(fā)事件,從而桌面圖標(biāo)數(shù)字動態(tài)改變。當(dāng)我們清楚通知時(shí),清空數(shù)字。自己也調(diào)研了iOS的做法,他們只是通過調(diào)用系統(tǒng)的一個(gè)方法將消息數(shù)字傳進(jìn)去即可,做法類似于Android 通過發(fā)送廣播方式,和三星一樣。
那么,小米是如何做到累加的呢。
我們只需定義全局變量count,初始值設(shè)置為1,然后發(fā)送通知后,手動改變count值,count=count+1。
友情鏈接:
https://github.com/lixiangers/BadgeUtil
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法,結(jié)合實(shí)例形式分析了Android使用定時(shí)器實(shí)現(xiàn)popupwindow定時(shí)消失的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android開發(fā)之ListView列表刷新和加載更多實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之ListView列表刷新和加載更多實(shí)現(xiàn)方法,實(shí)例分析了ListView列表操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06android 預(yù)加載進(jìn)程的實(shí)現(xiàn)方法
這篇文章主要介紹了android 預(yù)加載進(jìn)程的實(shí)現(xiàn)方法,大家需要注意清單文件中注冊并制定 android:process 要預(yù)加載的進(jìn)程,需要的朋友可以參考下2024-05-05Android編程簡單實(shí)現(xiàn)撥號器功能的方法
這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)撥號器功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android撥號器功能的實(shí)現(xiàn)原理、步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android studio設(shè)計(jì)簡易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android studio設(shè)計(jì)簡易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn)
這篇文章主要介紹了Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android 菜單欄DIY實(shí)現(xiàn)效果詳解
這篇文章主要為大家介紹了Android 菜單欄DIY實(shí)現(xiàn)效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09