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

来源:互联网 发布:淘宝怎么发视频给卖家 编辑:程序博客网 时间:2024/06/04 19:10

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
<codeclass="hljs java"><spanclass="hljs-keyword">public <spanclass="hljs-class"><spanclass="hljs-keyword">class <spanclass="hljs-title">MainActivity <spanclass="hljs-keyword">extends <spanclass="hljs-title">Activity {
      <spanclass="hljs-comment">//必须使用,Activity启动页
      <spanclass="hljs-keyword">private <spanclass="hljs-keyword">final <spanclass="hljs-keyword">static String lancherActivityClassName = Welcome.class.getName();
       
    <spanclass="hljs-meta">@Override
    <spanclass="hljs-function"><spanclass="hljs-keyword">protected <spanclass="hljs-keyword">void <spanclass="hljs-title">onCreate<spanclass="hljs-params">(Bundle savedInstanceState) {
        <spanclass="hljs-keyword">super.onCreate(savedInstanceState);
        setContentView(R.layout.common_listview_layout);
    }
 
    <spanclass="hljs-meta">@Override
    <spanclass="hljs-function"><spanclass="hljs-keyword">protected <spanclass="hljs-keyword">void <spanclass="hljs-title">onResume<spanclass="hljs-params">() {
        <spanclass="hljs-keyword">super.onResume();
        sendBadgeNumber();
    }
 
    <spanclass="hljs-function"><spanclass="hljs-keyword">private <spanclass="hljs-keyword">void <spanclass="hljs-title">sendBadgeNumber<spanclass="hljs-params">() {
        String number = <spanclass="hljs-string">"35";
        <spanclass="hljs-keyword">if (TextUtils.isEmpty(number)) {
            number = <spanclass="hljs-string">"0";
        } <spanclass="hljs-keyword">else {
            <spanclass="hljs-keyword">int numInt = Integer.valueOf(number);
            number = String.valueOf(Math.max(<spanclass="hljs-number">0, Math.min(numInt, <spanclass="hljs-number">99)));
        }
 
        <spanclass="hljs-keyword">if (Build.MANUFACTURER.equalsIgnoreCase(<spanclass="hljs-string">"Xiaomi")) {
            sendToXiaoMi(number);
        } <spanclass="hljs-keyword">else <spanclass="hljs-keyword">if (Build.MANUFACTURER.equalsIgnoreCase(<spanclass="hljs-string">"samsung")) {
            sendToSony(number);
        } <spanclass="hljs-keyword">else <spanclass="hljs-keyword">if (Build.MANUFACTURER.toLowerCase().contains(<spanclass="hljs-string">"sony")) {
            sendToSamsumg(number);
        } <spanclass="hljs-keyword">else {
            Toast.makeText(<spanclass="hljs-keyword">this, <spanclass="hljs-string">"Not Support", Toast.LENGTH_LONG).show();
        }
    }
 
    <spanclass="hljs-function"><spanclass="hljs-keyword">private <spanclass="hljs-keyword">void <spanclass="hljs-title">sendToXiaoMi<spanclass="hljs-params">(String number) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = <spanclass="hljs-keyword">null;
        <spanclass="hljs-keyword">boolean isMiUIV6 = <spanclass="hljs-keyword">true;
        <spanclass="hljs-keyword">try {
            NotificationCompat.Builder builder = <spanclass="hljs-keyword">new NotificationCompat.Builder(<spanclass="hljs-keyword">this); 
            builder.setContentTitle(<spanclass="hljs-string">"您有"+number+<spanclass="hljs-string">"未读消息");
            builder.setTicker(<spanclass="hljs-string">"您有"+number+<spanclass="hljs-string">"未读消息");
            builder.setAutoCancel(<spanclass="hljs-keyword">true);
            builder.setSmallIcon(R.drawable.common_icon_lamp_light_red);
            builder.setDefaults(Notification.DEFAULT_LIGHTS);
            notification = builder.build(); 
            Class miuiNotificationClass = Class.forName(<spanclass="hljs-string">"android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField(<spanclass="hljs-string">"messageCount");
            field.setAccessible(<spanclass="hljs-keyword">true);
            field.set(miuiNotification, number);<spanclass="hljs-comment">// 设置信息数
            field = notification.getClass().getField(<spanclass="hljs-string">"extraNotification"); 
            field.setAccessible(<spanclass="hljs-keyword">true);
        field.set(notification, miuiNotification);  
        Toast.makeText(<spanclass="hljs-keyword">this, <spanclass="hljs-string">"Xiaomi=>isSendOk=>1", Toast.LENGTH_LONG).show();
        }<spanclass="hljs-keyword">catch (Exception e) {
            e.printStackTrace();
            <spanclass="hljs-comment">//miui 6之前的版本
            isMiUIV6 = <spanclass="hljs-keyword">false;
                Intent localIntent = <spanclass="hljs-keyword">new Intent(<spanclass="hljs-string">"android.intent.action.APPLICATION_MESSAGE_UPDATE");
                localIntent.putExtra(<spanclass="hljs-string">"android.intent.extra.update_application_component_name",getPackageName() + <spanclass="hljs-string">"/"+ lancherActivityClassName );
                localIntent.putExtra(<spanclass="hljs-string">"android.intent.extra.update_application_message_text",number);
                sendBroadcast(localIntent);
        }
        <spanclass="hljs-keyword">finally
        {
          <spanclass="hljs-keyword">if(notification!=<spanclass="hljs-keyword">null && isMiUIV6 )
           {
               <spanclass="hljs-comment">//miui6以上版本需要使用通知发送
            nm.notify(<spanclass="hljs-number">101010, notification); 
           }
        }
 
    }
 
    <spanclass="hljs-function"><spanclass="hljs-keyword">private <spanclass="hljs-keyword">void <spanclass="hljs-title">sendToSony<spanclass="hljs-params">(String number) {
        <spanclass="hljs-keyword">boolean isShow = <spanclass="hljs-keyword">true;
        <spanclass="hljs-keyword">if (<spanclass="hljs-string">"0".equals(number)) {
            isShow = <spanclass="hljs-keyword">false;
        }
        Intent localIntent = <spanclass="hljs-keyword">new Intent();
        localIntent.putExtra(<spanclass="hljs-string">"com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);<spanclass="hljs-comment">//是否显示
        localIntent.setAction(<spanclass="hljs-string">"com.sonyericsson.home.action.UPDATE_BADGE");
        localIntent.putExtra(<spanclass="hljs-string">"com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",lancherActivityClassName );<spanclass="hljs-comment">//启动页
        localIntent.putExtra(<spanclass="hljs-string">"com.sonyericsson.home.intent.extra.badge.MESSAGE", number);<spanclass="hljs-comment">//数字
        localIntent.putExtra(<spanclass="hljs-string">"com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",getPackageName());<spanclass="hljs-comment">//包名
        sendBroadcast(localIntent);
 
        Toast.makeText(<spanclass="hljs-keyword">this, <spanclass="hljs-string">"Sony," + <spanclass="hljs-string">"isSendOk", Toast.LENGTH_LONG).show();
    }
 
    <spanclass="hljs-function"><spanclass="hljs-keyword">private <spanclass="hljs-keyword">void <spanclass="hljs-title">sendToSamsumg<spanclass="hljs-params">(String number) 
    {
        Intent localIntent = <spanclass="hljs-keyword">new Intent(<spanclass="hljs-string">"android.intent.action.BADGE_COUNT_UPDATE");
        localIntent.putExtra(<spanclass="hljs-string">"badge_count", number);<spanclass="hljs-comment">//数字
        localIntent.putExtra(<spanclass="hljs-string">"badge_count_package_name", getPackageName());<spanclass="hljs-comment">//包名
        localIntent.putExtra(<spanclass="hljs-string">"badge_count_class_name",lancherActivityClassName ); <spanclass="hljs-comment">//启动页
        sendBroadcast(localIntent);
        Toast.makeText(<spanclass="hljs-keyword">this, <spanclass="hljs-string">"Samsumg," + <spanclass="hljs-string">"isSendOk", Toast.LENGTH_LONG).show();
    }
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

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

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

相互学习,共同进步!

联系方式:240803830

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

0 0
原创粉丝点击