Android 桌面圖標右上角顯示未讀消息數(shù)字
背景:
在Android原生系統(tǒng)中,眾所周知不支持桌面圖標顯示未讀消息提醒的數(shù)字,雖然第三方控件BadgeView可以實現(xiàn)應用內(nèi)的數(shù)字提醒。但對于系統(tǒng)的圖標,特別是app的logo圖標很難實現(xiàn)數(shù)字標志,即使是繪圖的方式不斷修改,但這種方式天生弊端,實用性很差。但幸運的是,一些強大的手機廠商(小米,三星,索尼)提供了私有的API,但也帶來了難度,API的不同就意味著代碼量的增加和兼容性問題更加突出。
現(xiàn)在我們來看看他們是如何實現(xiàn)的:
實現(xiàn)原理:
首先我們要明白 并不是應用本身處理對啟動圖標進行修改、圖標的動態(tài)修改的過程主要是在Launcher里面完成的.在應用安裝,更新,卸載的時候,都會有廣播發(fā)出,Launcher在LauncherApplication 中注冊廣播,在LauncherModel中處理接收到廣播的消息,重新加載更新應用信息(如:應用圖標、文字等)。但是原生的android系統(tǒng)是并不支持該特性的(及不能通過發(fā)送特定的系統(tǒng)廣播 達到動態(tài)修改啟動圖標的效果),但是在強大的第三方Android手機廠商(如:三星、小米)的系統(tǒng)源碼深度定制下、通過修改了Launcher源代碼,增加/注冊了新的廣播接收器用來接收應用發(fā)送來的未讀消息數(shù)廣播,接收到廣播后,系統(tǒng)將未讀消息的數(shù)目顯示事件交給Launcher去處理,調(diào)用相關方法去重繪應用的icon,最終達到動態(tài)更新應用圖標的效果。
示例代碼:
public class LauncherBadgeHelper { /** * Set badge count * 針對 Samsung / xiaomi / sony 手機有效 * * @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); } } /** * 向小米手機發(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ù)-->這種發(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); } } /** * 向索尼手機發(fā)送未讀消息數(shù)廣播 * 據(jù)說:需添加權(quán)限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"> [未驗證] * * @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); } /** * 向三星手機發(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ā)送廣播來實現(xiàn)的。
但是:小米MIUI6以后,改變了處理方式,棄用了發(fā)送廣播的方式,改為通過發(fā)送通知。
一、基本介紹
1、默認的情況
當app 向通知欄發(fā)送了一條通知 (通知不帶進度條并且用戶可以刪除的),那么桌面app icon角標就會顯示1.此時app顯示的角標數(shù)是和通知欄里app發(fā)送的通知數(shù)對應的,即向通知欄發(fā)送了多少通知就會顯示多少角標
二、實現(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é)新的方法如下:
/** * 向小米手機發(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之前的,還能實現(xiàn)MIUI6以后的。自己在開發(fā)的時候經(jīng)過測試,發(fā)現(xiàn)MIUI內(nèi)部對于相同的消息數(shù)字是不顯示的,由于我測試的時候用的是寫死的數(shù)字,導致走了很多彎路。還有,自己在查找資料的時候發(fā)現(xiàn)有許多朋友都遇到過這樣的問題,未讀消息數(shù)字只有在第一次安裝的時候才顯示,進入后再設置就沒有了,我估計都是因為數(shù)字相同造成的。
細想一下,MIUI這種做法也挺好的,消息數(shù)字和通知綁定,當來通知時觸發(fā)事件,從而桌面圖標數(shù)字動態(tài)改變。當我們清楚通知時,清空數(shù)字。自己也調(diào)研了iOS的做法,他們只是通過調(diào)用系統(tǒng)的一個方法將消息數(shù)字傳進去即可,做法類似于Android 通過發(fā)送廣播方式,和三星一樣。
那么,小米是如何做到累加的呢。
我們只需定義全局變量count,初始值設置為1,然后發(fā)送通知后,手動改變count值,count=count+1。
友情鏈接:
https://github.com/lixiangers/BadgeUtil
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
Android編程實現(xiàn)popupwindow定時消失的方法
這篇文章主要介紹了Android編程實現(xiàn)popupwindow定時消失的方法,結(jié)合實例形式分析了Android使用定時器實現(xiàn)popupwindow定時消失的相關操作技巧,需要的朋友可以參考下2018-01-01Android開發(fā)之ListView列表刷新和加載更多實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之ListView列表刷新和加載更多實現(xiàn)方法,實例分析了ListView列表操作的相關技巧,需要的朋友可以參考下2015-06-06Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android開發(fā)中如何去掉app標題欄的實現(xiàn)
這篇文章主要介紹了Android開發(fā)中如何去掉app標題欄的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04