background:
In Android native systems, it is well known that desktop icons are not supported to display numbers of unread messages reminders, although the third-party control BadgeView can implement digital reminders in the application. However, it is difficult to implement digital logos for system icons, especially app logo icons. Even if the drawing method is constantly modified, this method is inherently disadvantageous and has poor practicality. But fortunately, some powerful mobile phone manufacturers (Xiaomi, Samsung, Sony) provide private APIs, but they also bring difficulties. The difference in APIs means that the increase in code volume and compatibility issues are more prominent.
Now let's see how they implement it:
Implementation principle:
First of all, we need to understand that the application itself does not handle the process of modifying the startup icon and dynamically modifying the icons. The application is mainly completed in Launcher. When the application is installed, updated, and uninstalled, a broadcast will be issued. Launcher registers the broadcast in LauncherApplication, processes the received broadcast messages in LauncherModel, and reloads and updates the application information (such as application icons, text, etc.). However, the native Android system does not support this feature (and cannot dynamically modify the startup icon by sending specific system broadcasts). However, under the deep customization of the system source code of powerful third-party Android mobile phone manufacturers (such as Samsung and Xiaomi), by modifying the Launcher source code, a new broadcast receiver is added/registered to receive the number of unread messages sent by the application. After receiving the broadcast, the system handed over the display event of the number of unread messages to Launcher for processing, and called relevant methods to redraw the application icon, and finally achieved the effect of dynamically updating the application icon.
Sample code:
public class LauncherBadgeHelper { /** * Set badge count * Valid for Samsung / xiaomi / sony phones * * @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 = (0, (count, 99)); } if (("Xiaomi")) { sendToXiaoMi(context, count); } else if (("sony")) { sendToSony(context, count); } else if (().contains("samsung")) { sendToSamsumg(context, count); } else { sendToSamsumg(context, count); } } /** * Send unread messages to Xiaomi mobile phones to broadcast * * @param count */ private static void sendToXiaoMi(Context context, int count) { try { Class miuiNotificationClass = (""); Object miuiNotification = (); Field field = ().getDeclaredField("messageCount"); (true); (miuiNotification, (count == 0 ? "" : count)); // Set the number of information --> This kind of sending must be miui 6 } catch (Exception e) { (()); // Miui 6 versions Intent localIntent = new Intent( ".APPLICATION_MESSAGE_UPDATE"); ( ".update_application_component_name", () + "/" + getLauncherClassName(context)); ( ".update_application_message_text", (count == 0 ? "" : count)); (localIntent); } } /** * Send unread messages to Sony phones to broadcast * It is said: permission needs to be added: <uses-permission android:name=".BROADCAST_BADGE"> [Not verified] * * @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(); (".UPDATE_BADGE"); (".SHOW_MESSAGE", isShow);// Whether to display (".ACTIVITY_NAME", launcherClassName);//Startup page ("", (count));//number (".PACKAGE_NAME", ());//Bail name (localIntent); } /** * Send unread messages to Samsung mobile phones * * @param count */ private static void sendToSamsumg(Context context, int count) { String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } Intent intent = new Intent(".BADGE_COUNT_UPDATE"); ("badge_count", count); ("badge_count_package_name", ()); ("badge_count_class_name", launcherClassName); (intent); } /** * Reset and clear Badge unread display number * * @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 = (); Intent intent = new Intent(Intent.ACTION_MAIN); // To limit the components this Intent will resolve to, by setting an // explicit package name. (()); (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 = (intent, 0); } return ; } }</uses-permission>
It can be seen that Xiaomi, Samsung and Sony's processing methods are all achieved by sending broadcasts.
However: After Xiaomi MIUI6, the processing method was changed, the broadcast method was deprecated, and the notification was sent instead.
1. Basic introduction
1. The default situation
When the app sends a notification to the notification bar (the notification does not have a progress bar and can be deleted by the user), the desktop app icon corner mark will be displayed 1. At this time, the number of corner marks displayed by the app corresponds to the number of notifications sent by the app in the notification bar, that is, the number of corner marks displayed as many notifications sent to the notification bar
2. Implement code
Third-party apps need to be called with reflection, reference code:
NotificationManager mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); builder = new (this) .setContentTitle(“title”).setContentText(“text”).setSmallIcon(); Notification notification = (); try { Field field = ().getDeclaredField(“extraNotification”); Object extraNotification = (notification); Method method = ().getDeclaredMethod(“setMessageCount”, ); (extraNotification, mCount); } catch (Exception e) { (); } (0,notification);
The new method I summarized in my previous code based on the official code is as follows:
/** * Send unread messages to Xiaomi mobile phones and broadcast miui6 after * * @param count */ private static void sendToXiaoMi2(Context context, int count) { NotificationManager mNotificationManager = (NotificationManager) ().getSystemService(Context.NOTIFICATION_SERVICE); builder = new (()).setContentTitle("title").setContentText("text").setSmallIcon(.ico_haoyilogo); Notification notification = null; if (.SDK_INT >= .VERSION_CODES.JELLY_BEAN) { notification = (); } try { Field field = ().getDeclaredField("extraNotification"); Object extraNotification = (notification); Method method = ().getDeclaredMethod("setMessageCount", ); (extraNotification, count); (10, notification); } catch (Exception e) { (); (()); // Miui 6 versions Intent localIntent = new Intent( ".APPLICATION_MESSAGE_UPDATE"); ( ".update_application_component_name", () + "/" + getLauncherClassName(context)); ( ".update_application_message_text", (count == 0 ? "" : count)); (localIntent); } }
This can not only be compatible with those before MIUI6, but also realize those after MIUI6. After testing during my development, I found that the same message numbers are not displayed in MIUI. Because I used dead numbers to write them when I was testing, I took many detours. Also, when I was looking for information, I found that many friends had encountered such problems. The unread message numbers were only displayed when they were installed for the first time, and they were gone after entering. I guess they were all caused by the same numbers.
Think about it carefully, MIUI is also very good. The message numbers are bound to notifications, and the event is triggered when notifications are made, so that the desktop icon numbers are dynamically changed. When we clearly inform, clear the numbers. I have also investigated the iOS approach. They just pass the message numbers in by calling a system method. The approach is similar to Android's broadcast method, just like Samsung.
So, how does Xiaomi accumulate?
We just need to define the global variable count, set the initial value to 1, and then manually change the count value after sending the notification, count=count+1.
Friendly links:
/lixiangers/BadgeUtil
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!