Andoid 桌面icon添加数字角标

产品经理拿着iPhone对我说,“这加上角标”,转身就走了。
在Android系统中,是不支持BadgeNumber的,也就是原生Android系统是没有类似于ios 桌面icon右上角数字提示角标的,国产厂商深度定制的Rom很多借鉴了ios角标的设计,提供了一些api,这也给实现提供了可能。

查看了可能支持厂家的文档:

问:在其他非华为手机上是否可以使用华为桌面角标功能?
答:不可以,只能支持华为设备的功能,且系统版本必须在EMUI4.1及以上。
问:华为角标对哪类应用开放?
答:从保护用户体验的角度出发,华为角标暂时只对较大型的纯即时通讯类应用(例:聊天工具、邮箱)和大型企业的内部办公应用开放,还请各位开发者谅解!(2016 07 04)


MIUI6&7桌面角标开源代码简介 MIUI6&7上重新设计了桌面app图标的角标显示,基本规则如下:

一、基本介绍

1、默认的情况

当app 向通知栏发送了一条通知 (通知不带进度条并且用户可以删除的),那么桌面app
icon角标就会显示1.此时app显示的角标数是和通知栏里app发送的通知数对应的,即向通知栏发送了多少通知就会显示多少角标。

2、通知可以定义角标数

例如 有5封未读邮件,通知栏里只会显示一条通知,但是想让角标显示5. 可以在发通知时加个标示。

二、实现代码

第三方app需要用反射来调用,参考代码:

NotificationManager mNotificationManager = (NotificationManager) this

.getSystemService(Context.NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(this)

.setContentTitle(“title”).setContentText(“text”).setSmallIcon(R.drawable.icon);

Notification notification = builder.build();

try {

Field field =
notification.getClass().getDeclaredField(“extraNotification”);

Object extraNotification = field.get(notification);

Method method =
extraNotification.getClass().getDeclaredMethod(“setMessageCount”,
int.class);

method.invoke(extraNotification, mCount);

} catch (Exception e) { e.printStackTrace(); }
mNotificationManager.notify(0,notification);

    /**
     * 创建通知小米系统设置桌面icon数字角标
     * 官网文档
     * @param context
     * @param title
     * @param content
     * @param number
     */
    public static void sendIconNumber(Context context, String title, String content, int number) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(title).setContentText(content).setSmallIcon(R.mipmap.ic_launcher);
        //设置通知点击后意图
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentIntent(pendingIntent);

        Notification notification = builder.build();

        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, number);

        } catch (Exception e) {
            e.printStackTrace();
        }
        mNotificationManager.notify(0, notification);
    }

最后:

整理的角标数字设置工具类

    /**
     * 设置桌面Icon角标,当前支持小米,索尼,三星。
     * @param context
     * @param number
     */
    public static void sendBadgeNumber(Context context,String number) {
        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(context,number);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
            sendToSony(context,number);
        } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
            sendToSamsumg(context,number);
        } else {
            Log.e("AppUtils", "sendBadgeNumber: Not Support" );
        }
    }
    private final static String lancherActivityClassName = SplashActivity.class.getName();
    private static void sendToXiaoMi(Context context,String number) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = null;
        boolean isMiUIV6 = true;
        try {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle("您有"+number+"未读消息");
            builder.setTicker("您有"+number+"未读消息");
            builder.setAutoCancel(true);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            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, Integer.valueOf(number));// 设置信息数
            field = notification.getClass().getField("extraNotification");
            field.setAccessible(true);
            field.set(notification, miuiNotification);
        }catch (Exception e) {
            e.printStackTrace();
            //miui 6之前的版本
            isMiUIV6 = false;
            Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
            localIntent.putExtra("android.intent.extra.update_application_component_name",context.getPackageName() + "/"+ lancherActivityClassName );
            localIntent.putExtra("android.intent.extra.update_application_message_text",number);
            context.sendBroadcast(localIntent);
        }
        finally
        {
            if(notification!=null && isMiUIV6 )
            {
                //miui6以上版本需要使用通知发送
                nm.notify(101010, notification);
            }
        }

    }

    private static  void sendToSony(Context context,String number) {
        boolean isShow = true;
        if ("0".equals(number)) {
            isShow = false;
        }
        Intent localIntent = new Intent();
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);//是否显示
        localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",lancherActivityClassName );//启动页
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number);//数字
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",context.getPackageName());//包名
        context.sendBroadcast(localIntent);
    }

    private static void sendToSamsumg(Context context,String number)
    {
        Intent localIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        localIntent.putExtra("badge_count", Integer.valueOf(number));//数字
        localIntent.putExtra("badge_count_package_name", context.getPackageName());//包名
        localIntent.putExtra("badge_count_class_name",lancherActivityClassName ); //启动页
        context.sendBroadcast(localIntent);
        Log.d("AppUtils","Samsumg isSendOk"+number);
    }

使用

 AppUtils.sendBadgeNumber(this,"");//清空
 AppUtils.sendBadgeNumber(this,"35");//设置角标显示

这里写图片描述

参看文章:http://my.oschina.net/ososchina/blog/352286

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值