Android system Xiaomi/Samsung/Sony App start icon unread message count (badgenumber) Dynamic alert

Source: Internet
Author: User

On Android phones, such as QQ, when there are unread messages, we can see in the app's launch icon in the upper right corner there will be a red circle, and the circle will dynamically display the number of unread messages, such as display:

So how does this function be implemented?
In the universal Internet search and read a lot of relevant information, but also asked some of the technology group of the big players. From them I learned and extracted some key words: third-party control Badgeview (Implementation of digital reminders within the app), shortcut icons, Launcher, Reflections.
The odds and ends of spending nearly a day, and finally figured out. Wrote a demo test program to verify and self-check. The demo effect is as follows:
The test results on the Samsung Galaxy S4 are as follows:

The test results on the Xiaomi phone are as follows:

Implementation principle:
First of all, we have to understand that it is not the application itself process to modify the boot icon, the dynamic modification of the icon is mainly done in the launcher. When the application installs, updates, uninstalls, there will be broadcasts, launcher registered in the Launcherapplication broadcast , process messages received in Launchermodel, Reload Update app information (e.g. app icon, text, etc.). However, the native Android system does not support this feature (and can not send a specific system broadcast to dynamically modify the effect of the start icon), but in the powerful third-party Android phone manufacturers (such as: Samsung, Xiaomi) system source depth customization, by modifying the launcher source code, Added/registered a new broadcast receiver is used to receive the application sent by the number of unread messages broadcast, after receiving the broadcast, the system will not read the number of messages displayed event to launcher to process, call the relevant method to redraw the app's icon, and finally achieve the effect of dynamically updating the application icons.

After understanding the implementation principle, we probably understand the whole process is this (except the native system):
In the third party handset manufacturer's ROM, if modifies the launcher source code and supports the above mentioned unread message number broadcast reception, then we simply send in the application to let the system receive the broadcast to be able to realize this article to achieve the effect in this device's handset.
But third-party handset manufacturers of such broadcasts are certainly different conditions, so the most important thing is to know the handset manufacturers of such broadcast intent receiving conditions.
Fortunately, you can always find what you need on the almighty Internet, the following encapsulates a tool class Badgeutil.java implements the broadcast of unread messages from different handset manufacturers. The specific code is as follows:

ImportJava.lang.reflect.Field;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.pm.PackageManager;ImportAndroid.content.pm.ResolveInfo;ImportAndroid.os.Build;ImportAndroid.widget.Toast;/** * App start icon Unread message number Display tool class (effects such as: QQ, unread SMS and other application icons) <br/> * dependent on third-party handset manufacturers (such as: Xiaomi, Samsung) launcher customization, native system does not support this feature <br/> * The tool class supports devices such as Xiaomi, Samsung, Sony "where Xiaomi, Samsung pro-Test effective, Sony unverified" * @author [email protected] * */ Public  class badgeutil {    /** * Set Badge count<br/> * valid for samsung/xiaomi/sony phone * @param context the context O     f the application package. * @param count Badge count to be set * /     Public Static void Setbadgecount(Context context,intCount) {if(Count <=0) {count =0; }Else{count = Math.max (0, Math.min (Count, About)); }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{Toast.maketext (context," not support", Toast.length_long). Show (); }    }/** * Send unread messages to Xiaomi phone broadcast * @param Count */    Private Static void Sendtoxiaomi(Context context,intCount) {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));//Set the number of messages-the send must be MIUI 6.}Catch(Exception e) {E.printstacktrace ();//MIUI version prior to 6Intent localintent =NewIntent ("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); }    }/** * Send unread messages to Sony phone broadcast <br/> * It is said that 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,intCount) {String Launcherclassname = getlauncherclassname (context);if(Launcherclassname = =NULL) {return; }BooleanIsshow =true;if(Count = =0) {isshow =false; } Intent localintent =NewIntent (); Localintent.setaction ("Com.sonyericsson.home.action.UPDATE_BADGE"); Localintent.putextra ("Com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isshow);//whether to displayLocalintent.putextra ("Com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherclassname);//Start PageLocalintent.putextra ("Com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueof (count));//DigitalLocalintent.putextra ("Com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", Context.getpackagename ());//Package nameContext.sendbroadcast (localintent); }/** * Send unread messages to Samsung phones broadcast * @param Count */    Private Static void SENDTOSAMSUMG(Context context,intCount) {String Launcherclassname = getlauncherclassname (context);if(Launcherclassname = =NULL) {return; } Intent Intent =NewIntent ("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); }/** * Reset, clear badge unread display number <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 StaticStringGetlauncherclassname(Context context)        {Packagemanager Packagemanager = Context.getpackagemanager (); Intent Intent =NewIntent (Intent.action_main);//To limit the components of this Intent would resolve to, by setting an        //Explicit package name.Intent.setpackage (Context.getpackagename ()); Intent.addcategory (Intent.category_launcher);//All application must has 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 have filtered by Category_default        if(Info = =NULL) {info = packagemanager.resolveactivity (Intent,0); }returnInfo.activityInfo.name; }}

In the activity that is started, the number of messages that are sent unread and the number of messages that are reset/cleared are broadcast as follows:
Send unread message number broadcast: Count is the number of unread messages (int type)

count);

Send reset/Clear number of unread messages broadcast:

BadgeUtil.resetBadgeCount(getApplicationContext());

Data 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 the article to help you, you can help the top, to support a bit oh!
If you have any questions about the content of the article or have a better opinion, please contact me by message or email:
[Email protected]

If you need to reprint, please indicate the source, thank you!!
—————————————————————————————————————

Android system Xiaomi/Samsung/Sony App start icon unread message count (badgenumber) Dynamic alert

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.