Android Icon数字角标(BadgeNumber)的实现方式

Android Icon数字角标(BadgeNumber)的实现方式

 

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

Android系统 小米,三星,索尼手机发送桌面快键提醒数字图标,在Android系统中,众所周知不支持BadgeNumber,虽然第三方控件BadgeView可以实现应用内的数字提醒,但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。但幸运的是,某些ROM厂商提供了私有的API,但也带来了难度,API的不同意意味着代码量的增加和兼容性问题更加突出。

我们现在来实现桌面logo或者说icon右上角的图标,先来看2张图,第一张来自互联网,第二张来自个人实践!(由于实验条件有限,只能测试小米的(⊙o⊙)…,有兴趣的同学测试一下其他的吧)

    

好了,上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class = "hljs java" > class = "hljs-keyword" > public  class = "hljs-class" > class = "hljs-keyword" > class  class = "hljs-title" >MainActivity  class = "hljs-keyword" > extends  class = "hljs-title" >Activity {
       class = "hljs-comment" > //必须使用,Activity启动页
       class = "hljs-keyword" > private  class = "hljs-keyword" > final  class = "hljs-keyword" > static  String lancherActivityClassName = Welcome. class .getName();
       
     class = "hljs-meta" > @Override
     class = "hljs-function" > class = "hljs-keyword" > protected  class = "hljs-keyword" > void  class = "hljs-title" >onCreate class = "hljs-params" >(Bundle savedInstanceState) {
         class = "hljs-keyword" > super .onCreate(savedInstanceState);
         setContentView(R.layout.common_listview_layout);
     }
 
     class = "hljs-meta" > @Override
     class = "hljs-function" > class = "hljs-keyword" > protected  class = "hljs-keyword" > void  class = "hljs-title" >onResume class = "hljs-params" >() {
         class = "hljs-keyword" > super .onResume();
         sendBadgeNumber();
     }
 
     class = "hljs-function" > class = "hljs-keyword" > private  class = "hljs-keyword" > void  class = "hljs-title" >sendBadgeNumber class = "hljs-params" >() {
         String number =  class = "hljs-string" > "35" ;
         class = "hljs-keyword" > if  (TextUtils.isEmpty(number)) {
             number =  class = "hljs-string" > "0" ;
         class = "hljs-keyword" > else  {
             class = "hljs-keyword" > int  numInt = Integer.valueOf(number);
             number = String.valueOf(Math.max( class = "hljs-number" > 0 , Math.min(numInt,  class = "hljs-number" > 99 )));
         }
 
         class = "hljs-keyword" > if  (Build.MANUFACTURER.equalsIgnoreCase( class = "hljs-string" > "Xiaomi" )) {
             sendToXiaoMi(number);
         class = "hljs-keyword" > else  class = "hljs-keyword" > if  (Build.MANUFACTURER.equalsIgnoreCase( class = "hljs-string" > "samsung" )) {
             sendToSony(number);
         class = "hljs-keyword" > else  class = "hljs-keyword" > if  (Build.MANUFACTURER.toLowerCase().contains( class = "hljs-string" > "sony" )) {
             sendToSamsumg(number);
         class = "hljs-keyword" > else  {
             Toast.makeText( class = "hljs-keyword" > this class = "hljs-string" > "Not Support" , Toast.LENGTH_LONG).show();
         }
     }
 
     class = "hljs-function" > class = "hljs-keyword" > private  class = "hljs-keyword" > void  class = "hljs-title" >sendToXiaoMi class = "hljs-params" >(String number) {
         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         Notification notification =  class = "hljs-keyword" > null ;
         class = "hljs-keyword" > boolean  isMiUIV6 =  class = "hljs-keyword" > true ;
         class = "hljs-keyword" > try  {
             NotificationCompat.Builder builder =  class = "hljs-keyword" > new  NotificationCompat.Builder( class = "hljs-keyword" > this ); 
             builder.setContentTitle( class = "hljs-string" > "您有" +number+ class = "hljs-string" > "未读消息" );
             builder.setTicker( class = "hljs-string" > "您有" +number+ class = "hljs-string" > "未读消息" );
             builder.setAutoCancel( class = "hljs-keyword" > true );
             builder.setSmallIcon(R.drawable.common_icon_lamp_light_red);
             builder.setDefaults(Notification.DEFAULT_LIGHTS);
             notification = builder.build(); 
             Class miuiNotificationClass = Class.forName( class = "hljs-string" > "android.app.MiuiNotification" );
             Object miuiNotification = miuiNotificationClass.newInstance();
             Field field = miuiNotification.getClass().getDeclaredField( class = "hljs-string" > "messageCount" );
             field.setAccessible( class = "hljs-keyword" > true );
             field.set(miuiNotification, number); class = "hljs-comment" > // 设置信息数
             field = notification.getClass().getField( class = "hljs-string" > "extraNotification" ); 
             field.setAccessible( class = "hljs-keyword" > true );
         field.set(notification, miuiNotification);  
         Toast.makeText( class = "hljs-keyword" > this class = "hljs-string" > "Xiaomi=>isSendOk=>1" , Toast.LENGTH_LONG).show();
         } class = "hljs-keyword" > catch  (Exception e) {
             e.printStackTrace();
             class = "hljs-comment" > //miui 6之前的版本
             isMiUIV6 =  class = "hljs-keyword" > false ;
                 Intent localIntent =  class = "hljs-keyword" > new  Intent( class = "hljs-string" > "android.intent.action.APPLICATION_MESSAGE_UPDATE" );
                 localIntent.putExtra( class = "hljs-string" > "android.intent.extra.update_application_component_name" ,getPackageName() +  class = "hljs-string" > "/" + lancherActivityClassName );
                 localIntent.putExtra( class = "hljs-string" > "android.intent.extra.update_application_message_text" ,number);
                 sendBroadcast(localIntent);
         }
         class = "hljs-keyword" > finally
         {
           class = "hljs-keyword" > if (notification!= class = "hljs-keyword" > null  && isMiUIV6 )
            {
                class = "hljs-comment" > //miui6以上版本需要使用通知发送
             nm.notify( class = "hljs-number" > 101010 , notification); 
            }
         }
 
     }
 
     class = "hljs-function" > class = "hljs-keyword" > private  class = "hljs-keyword" > void  class = "hljs-title" >sendToSony class = "hljs-params" >(String number) {
         class = "hljs-keyword" > boolean  isShow =  class = "hljs-keyword" > true ;
         class = "hljs-keyword" > if  ( class = "hljs-string" > "0" .equals(number)) {
             isShow =  class = "hljs-keyword" > false ;
         }
         Intent localIntent =  class = "hljs-keyword" > new  Intent();
         localIntent.putExtra( class = "hljs-string" > "com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE" ,isShow); class = "hljs-comment" > //是否显示
         localIntent.setAction( class = "hljs-string" > "com.sonyericsson.home.action.UPDATE_BADGE" );
         localIntent.putExtra( class = "hljs-string" > "com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME" ,lancherActivityClassName ); class = "hljs-comment" > //启动页
         localIntent.putExtra( class = "hljs-string" > "com.sonyericsson.home.intent.extra.badge.MESSAGE" , number); class = "hljs-comment" > //数字
         localIntent.putExtra( class = "hljs-string" > "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME" ,getPackageName()); class = "hljs-comment" > //包名
         sendBroadcast(localIntent);
 
         Toast.makeText( class = "hljs-keyword" > this class = "hljs-string" > "Sony,"  class = "hljs-string" > "isSendOk" , Toast.LENGTH_LONG).show();
     }
 
     class = "hljs-function" > class = "hljs-keyword" > private  class = "hljs-keyword" > void  class = "hljs-title" >sendToSamsumg class = "hljs-params" >(String number) 
     {
         Intent localIntent =  class = "hljs-keyword" > new  Intent( class = "hljs-string" > "android.intent.action.BADGE_COUNT_UPDATE" );
         localIntent.putExtra( class = "hljs-string" > "badge_count" , number); class = "hljs-comment" > //数字
         localIntent.putExtra( class = "hljs-string" > "badge_count_package_name" , getPackageName()); class = "hljs-comment" > //包名
         localIntent.putExtra( class = "hljs-string" > "badge_count_class_name" ,lancherActivityClassName );  class = "hljs-comment" > //启动页
         sendBroadcast(localIntent);
         Toast.makeText( class = "hljs-keyword" > this class = "hljs-string" > "Samsumg,"  class = "hljs-string" > "isSendOk" , Toast.LENGTH_LONG).show();
     }
}

注意lancherActivityClassName 必须被配置为 启动页   android.intent.category.LAUNCHER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class = "hljs xml" class = "hljs-tag" >< class = "hljs-name" >activity
             class = "hljs-attr" >android:name= class = "hljs-string" > "com.sample.activites.Welcome"
             class = "hljs-attr" >android:configChanges= class = "hljs-string" > "locale|keyboard|screenSize"
             class = "hljs-attr" >android:label= class = "hljs-string" > "@string/app_name"
             class = "hljs-attr" >android:screenOrientation= class = "hljs-string" > "portrait"  >
             class = "hljs-tag" >< class = "hljs-name" >intent-filter>
                 class = "hljs-tag" >< class = "hljs-name" >action  class = "hljs-attr" >android:name= class = "hljs-string" > "android.intent.action.MAIN"  />
 
                 class = "hljs-tag" >< class = "hljs-name" >category  class = "hljs-attr" >android:name= class = "hljs-string" > "android.intent.category.LAUNCHER"  />
             class = "hljs-tag" > class = "hljs-name" >intent-filter>
             class = "hljs-tag" >< class = "hljs-name" >intent-filter>
                 class = "hljs-tag" >< class = "hljs-name" >action  class = "hljs-attr" >android:name= class = "hljs-string" > "android.intent.action.CREATE_SHORTCUT"  />
             class = "hljs-tag" > class = "hljs-name" >intent-filter>
         class = "hljs-tag" > class = "hljs-name" >activity>

相互学习,共同进步!

联系方式:240803830

主页:http://www.cnblogs.com/taoboy/

你可能感兴趣的:(Android Icon数字角标(BadgeNumber)的实现方式)