Desktop Icon Unread message ( Sony, Samsung Mobile)

Source: Internet
Author: User

New message coming up, showing the number of new messages on the desktop Laucher icon

/*** Application Desktop Icon Unread message Display Tool class * Only support Xiaomi, Samsung and Sony*/ Public classBadgeutil {Final StaticString launcher_activity_name = "Com.wenki.example.activity.SplashActivity";  Public Static voidSetbadgecount (Context context,intcount) {        if(Count <= 0) {Count= 0; } Else{Count= Math.max (0, Math.min (count, 99)); }        if(Build.MANUFACTURER.equalsIgnoreCase ("Sony") {sendtosony (context, count); } Else if(Build.MANUFACTURER.toLowerCase (). Contains ("Samsung") {SENDTOSAMSUMG (context, count); } Else if(ISMIUI ()) {Sendtoxiaomi (context, count); } Else{LOGUTILS.D ("Badgeutil:badge not support"); }    }     Public Static voidClearbadgecount (Context context) {Setbadgecount (context),0); }    Private Static voidSendtoxiaomi (Context context,intcount) {Notificationmanager Mnotificationmanager=(Notificationmanager) context. Getsystemservice (Context.notification_service); Notification.builder Builder=NewNotification.builder (context). Setcontenttitle ("Title"). Setcontenttext ("Content Text")). Setsmallicon (R.drawable.app_icon); Try{Notification Notification=builder.getnotification (); 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 (1001, notification); } Catch(Exception e) {Intent localintent=NewIntent ("Android.intent.action.APPLICATION_MESSAGE_UPDATE"); Localintent.putextra ("Android.intent.extra.update_application_component_name", Context.getpackagename ()+ "/" +launcher_activity_name); Localintent.putextra ("Android.intent.extra.update_application_message_text", string.valueof (Count= = 0? "": Count));        Context.sendbroadcast (localintent); }    }    Private Static voidSendtosony (Context context,intcount) {        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);//is displayedLocalintent.putextra ("Com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcher_activity_name);//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); }    Private Static voidSENDTOSAMSUMG (Context context,intcount) {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", Launcher_activity_name);    Context.sendbroadcast (Intent); }    Private StaticString Getlauncherclassname (context context) {Packagemanager pm=Context.getpackagemanager (); Intent Intent=NewIntent (Intent.action_main);        Intent.addcategory (Intent.category_launcher); List<ResolveInfo> Resolveinfos = pm.queryintentactivities (Intent, 0);  for(ResolveInfo resolveinfo:resolveinfos) {String pkgname=ResolveInfo.activityInfo.applicationInfo.packageName; if(Pkgname.equalsignorecase (Context.getpackagename ())) {String ClassName=ResolveInfo.activityInfo.name; returnClassName; }        }        return NULL; }    Private Static BooleanIsmiui () {String code= Getsystemproperty ("Ro.miui.ui.version.code"); String name= Getsystemproperty ("Ro.miui.ui.version.name"); return! Textutils.isempty (code) &&!textutils.isempty (name); }     Public Staticstring Getsystemproperty (String propname) {string line; BufferedReader input=NULL; Try{Process P= Runtime.getruntime (). EXEC ("Getprop" +propname); Input=NewBufferedReader (NewInputStreamReader (P.getinputstream ()), 1024); Line=Input.readline ();        Input.close (); } Catch(IOException ex) {return NULL; } finally {            if(Input! =NULL) {                Try{input.close (); } Catch(IOException e) {//e.printstacktrace ();                }            }        }        returnLine ; }}

Where Launcher_activity_name is the start page or can be obtained using the Getlauncherclassname () method.

Xiaomi's MIUI6.0 and above versions must be implemented using the notification bar, which is the form of a broadcast before 6.0. The Ismiui () method determines all the MIUI systems, including the brush machine.

Sony does not have a test machine, not tested. Xiaomi Samsung Test effective

Desktop Icon Unread message (Xiaomi, Sony, Samsung Mobile)

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.