Android system Samsung/Sony app startup icon unread message count (BadgeNumber) Dynamic reminder, androidbadgenumber

Source: Internet
Author: User

Android system Samsung/Sony app startup icon unread message count (BadgeNumber) Dynamic reminder, androidbadgenumber

On Android phones, for example, QQ, when there is unread message, we can see that there is a red circle in the upper right corner of the app startup icon, and the number of unread messages is dynamically displayed in the circle, as shown in:

How is this function implemented?
I have searched and read a lot of related information on the omnipotent Internet, and have also consulted some technical experts. I learned from them and extracted some keywords: third-party control BadgeView (implementing digital reminders in the application), quick icons, Launcher, and reflection.
It took nearly one day to finally figure it out. I wrote a demo test program for verification and self-testing. The demo effect is as follows:
The test results on Samsung Galaxy S4 are as follows:

The test results on mobile phone are as follows:

Implementation principle:
First of all, we need to understand that the process of modifying the startup icon and dynamically modifying the icon is not completed in the Launcher. when an application is installed, updated, or uninstalled, a broadcast is sent. Launcher registers a broadcast in the LauncherApplication, processes the received broadcast messages in the LauncherModel, and reloads the updated application information (such: application icons, text, etc ). However, the native android system does not support this feature (and cannot dynamically modify the startup icon by sending a specific system broadcast). However, in powerful third-party Android mobile phone manufacturers (such: under the in-depth customization of system source code of Samsung and, 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 submits the number of unread message display events to the Launcher for processing, and calls related methods to re-paint the application icon to achieve dynamic update of the application icon.

After understanding the implementation principle, we will probably understand that the entire process is like this (except for native systems ):
In the ROM of a third-party mobile phone manufacturer, if you modify the Launcher source code and support the reception of the number of unread messages broadcast mentioned above, you only need to send a broadcast in the application that can be received by the system. you can achieve the desired effect on the mobile phone of this device.
However, the conditions for receiving such broadcasts from third-party mobile phone manufacturers must be different. Therefore, the most important thing is to know the Intent receiving conditions of such broadcasts from mobile phone manufacturers.
Fortunately, you can always find what you need on the omnipotent Internet. The following encapsulates a tool class BadgeUtil. java to broadcast the number of unread messages of different mobile phone manufacturers. The Code is as follows:

Import java. lang. reflect. field; import android. content. context; import android. content. intent; import android. content. pm. packageManager; import android. content. pm. resolveInfo; import android. OS. build; import android. widget. toast;/*** application startup icon unread message Count display tool class (effects such as QQ, unread SMS, and other app icons) <br/> * dependent on third-party mobile phone manufacturers (such: Samsung) launcher customization, native systems do not support this feature <br/> * this tool supports the following devices: Samsung, and Sony [effective for and Samsung, not verified by Sony ]* @ author ice_zhengbin@163.com **/pu Blic class BadgeUtil {/*** Set badge count <br/> * Valid for Samsung/sony mobile 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 = Math. max (0, Math. min (count, 99);} if (Build. MANUFACTURER. equalsIgnoreCase{sendTo (context, Count);} else if (Build. MANUFACTURER. inclusignorecase ("sony") {sendToSony (context, count);} else if (Build. MANUFACTURER. toLowerCase (). contains ("samsung") {sendToSamsumg (context, count);} else {Toast. makeText (context, "Not Support", Toast. LENGTH_LONG ). show () ;}}/*** send unread message count broadcast to mobile phone * @ param count */private static void sendTo(Context context, int count) {try {Class miuiNotificati OnClass = 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); // set the number of messages --> this type of sending must be miui 6} catch (Exception e) {e. printStackTrace (); // 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_messa Ge_text ", String. valueOf (count = 0? "": Count); context. sendBroadcast (localIntent) ;}}/*** send unread message count broadcast to Sony mobile phone <br/> * said: You need to add permission: <uses-permission android: name = "com. sonyericsson. home. permission. 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 (); localIntent. setAction ("com. sonyericsson. home. action. UPDATE_BADGE "); localIntent. putExtra ("com. sonyericsson. home. intent. extra. badge. SHOW_MESSAGE ", isShow); // whether to display localIntent. putExtra ("com. sonyericsson. home. intent. extra. badge. ACTIVITY_NAME ", launcherClassName); // localIntent on the startup page. putExtra ("com. sonyericsson. home. intent. extra. badge. MESSAGE ", String. va LueOf (count); // number localIntent. putExtra ("com. sonyericsson. home. intent. extra. badge. PACKAGE_NAME ", context. getPackageName (); // package name context. sendBroadcast (localIntent);}/*** send unread message count broadcast to Samsung mobile phone * @ param count */private static void sendToSamsumg (Context context, int count) {String launcherClassName = getLauncherClassName (context); if (launcherClassName = null) {return;} Intent intent = new Intent ("and Roid. 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);}/*** reset and clear unread Badge display count <br/> * @ 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 ;}}

In the started Activity, the call to broadcast the number of unread messages and reset/clear the number of unread messages is as follows:
// Number of unread messages sent broadcast: count indicates the number of unread messages (int type)

BadgeUtil.setBadgeCount(getApplicationContext(), count);

// Send reset/clear the number of unread messages broadcast:

BadgeUtil.resetBadgeCount(getApplicationContext());

Reference:
Http://blog.csdn.net/andylao62/article/details/41794695
Http://blog.csdn.net/wx_962464/article/details/37997299
Https://github.com/ekinlyw/android-badge
Http://www.tuicool.com/articles/JV7vIr

-------------------------------------
If the content of this article is helpful to you, you can help me to support it!
If you have any questions or have better opinions on the content of the article, please contact me by leaving a message or sending an email:
Ice_zhengbin@163.com

If you need to reprint it, please indicate the source. Thank you !!
-------------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.