How to implement the Android icon Digital Corner label (Badgenumber)

Source: Internet
Author: User

http://blog.csdn.net/janice0529/article/details/44344169

Android system Xiaomi, Samsung, Sony mobile phone send desktop quick key reminder digital icon, in Android system, well-known does not support Badgenumber, although third-party control Badgeview can implement in-app digital reminders, but for system icons, In particular, the logo of the app is difficult to achieve digital signage, even the way the drawings are constantly modified, but this way is inherently flawed, very poor practicality. Fortunately, some ROM vendors offer private APIs, but they also make it difficult, and the API's disagreement means that the increase in code volume and compatibility issues are more pronounced.

We now come to realize the desktop logo or icon on the top right corner, first look at 2 pictures, the first one from the Internet, the second from the personal practice! (because the experimental conditions are limited, only to test millet (⊙o⊙) ..., interested students to test the other bar)

Okay, on the code.

PublicClassMainactivityExtendsActivity {Must use, Activity start PagePrivateFinalstatic String Lancheractivityclassname = Welcome.class.getName ();@OverrideProtectedvoidOnCreate(Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (r.layout.common_listview_layout);}@OverrideProtectedvoidOnresume() {Super.onresume (); Sendbadgenumber ();}PrivatevoidSendbadgenumber() {String number ="35";if (textutils.isempty (number)) {Number ="0";}else {int numint = integer.valueof (number); number = String.valueof (Math.max (0, Math.min (numint,99)));}if (Build.MANUFACTURER.equalsIgnoreCase ("Xiaomi")) {Sendtoxiaomi (number);}Elseif (Build.MANUFACTURER.equalsIgnoreCase ("Samsung") {Sendtosony (number);}Elseif (Build.MANUFACTURER.toLowerCase (). Contains ("Sony") {SENDTOSAMSUMG (number);}else {Toast.maketext (This"Not support", Toast.length_long). Show ();}}PrivatevoidSendtoxiaomi(String number) {Notificationmanager nm = (Notificationmanager) getsystemservice (Context.notification_service); Notification Notification =NullBoolean isMiUIV6 =Truetry {notificationcompat.builder Builder =New Notificationcompat.builder (this); Builder.setcontenttitle ("You have" +number+"Unread message"); Builder.setticker ("You have" +number+"Unread message"); Builder.setautocancel (true); Builder.setsmallicon (r.drawable.common_icon_lamp_light_red); Builder.setdefaults (Notification.DEFAULT_ LIGHTS); notification = Builder.build (); Class Miuinotificationclass = Class.forName ("Android.app.MiuiNotification"); Object miuinotification = Miuinotificationclass.newinstance (); Field field = Miuinotification.getclass (). Getdeclaredfield ("Messagecount"); Field.setaccessible (true); Field.set (miuinotification, number);Set number of messages field = Notification.getclass (). GetField ("Extranotification"); Field.setaccessible (true);          Field.set (notification, miuinotification); Toast.maketext (This"Xiaomi=>issendok=>1", Toast.length_long). Show ();}catch (Exception e) {e.printstacktrace ();MIUI version prior to 6 isMiUIV6 =False Intent localintent =New Intent ("Android.intent.action.APPLICATION_MESSAGE_UPDATE"); Localintent.putextra ("Android.intent.extra.update_application_component_name", getpackagename () +"/" + lancheractivityclassname); Localintent.putextra ("Android.intent.extra.update_application_message_text", number); Sendbroadcast (localintent);}finally{if (notification!=Null && isMiUIV6) {Miui6 The above version requires the use of notifications to send Nm.notify (101010, notification); }}}PrivatevoidSendtosony(String number) {Boolean isshow =Trueif ("0". Equals (number) {isshow =false;} Intent localintent =New Intent (); Localintent.putextra ("Com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isshow);Whether to show Localintent.setaction ("Com.sonyericsson.home.action.UPDATE_BADGE"); Localintent.putextra ("Com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", lancheractivityclassname);Start Page Localintent.putextra ("Com.sonyericsson.home.intent.extra.badge.MESSAGE", number);Digital Localintent.putextra ("Com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", Getpackagename ());Package name Sendbroadcast (localintent); Toast.maketext (This"Sony," +"Issendok", Toast.length_long). Show ();}private void  Sendtosamsumg (String number)  {intent localintent = new intent ( "Android.intent.action.BADGE_COUNT_UPDATE"); Localintent.putextra ( "Badge_count",  number);  "Badge_count_package_name",  getpackagename ()); //package name Localintent.putextra ( "Badge_count_class_name", lancheractivityclassname );  //start Page sendbroadcast (localIntent); Toast.maketext (this,  "SAMSUMG,"  +  "Issendok",  toast.length_long). Show ();}}         

Note Lancheractivityclassname must be configured as the startup page Android.intent.category.LAUNCHER

<ActivityAndroid:name="Com.sample.activites.Welcome"android:configchanges="Locale|keyboard|screensize"Android:label="@string/app_name"android:screenorientation="Portrait" ><Intent-filter><ActionAndroid:name="Android.intent.action.MAIN"/><CategoryAndroid:name= "Android.intent.category.LAUNCHER"  />             </intent-filter>             <intent-filter>                 <action  Android:name= "Android.intent.action.CREATE_SHORTCUT"  />             </ Intent-filter>        </ ACTIVITY>             

How to implement the Android icon Digital Corner label (badgenumber)

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.