Android桌面图标实现

2017-03-17  本文已影响392人  Ernest_Chang

基于ShortcutBadger实现

The ShortcutBadger makes your Android App show the count of unread messages as a badge on your App shortcut!

img

ShortcutBadger使用

测试运行效果

最终代码

  /**
     * 生成通知和桌面角标
     */
    private void setFlowNotifaction(Context context, Intent resultIntent, String title, String msg) {

        mBadge++;

        // 小米miui6.0以后根据通知自动生成badge,根据通知次数自动累加。调用ShortcutBadger会自动生成空通知,需调用以下修改badge
        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi") && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            tryNewMiuiBadge(context, resultIntent, mBadge, title, msg, R.drawable.icon_app);
        } else {
            showNotifacitonBadge(context, resultIntent, mBadge, title, msg, R.drawable.icon_app);
        }
    }
    
    private void showNotifacitonBadge(Context context, Intent resultIntent, int badge, String title, String msg, int drawIcon) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(drawIcon)
                        .setContentTitle(title)
                        .setContentText(msg)
                        .setTicker(msg);
//        mBuilder.setFullScreenIntent(pendingIntent, true);
        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setAutoCancel(true);


        // 设置通知的优先级
        mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
//        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//        // 设置通知的提示音
//        mBuilder.setSound(alarmSound);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);


        int mNotificationId = 10086;
        NotificationManager mNotifyMgr = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
        // 生成桌面角标
        ShortcutBadger.applyCount(JApplication.getInstance(), badge);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void tryNewMiuiBadge(Context context, Intent resultIntent, int badgeCount, String title, String msg, int drawIcon) {

        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(drawIcon)
                        .setContentTitle(title)
                        .setContentText(msg)
                        .setTicker(msg);
//        mBuilder.setFullScreenIntent(pendingIntent, true);
        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setAutoCancel(true);


        // 设置通知的优先级
        mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
//        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//        // 设置通知的提示音
//        mBuilder.setSound(alarmSound);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);


//        int mNotificationId = 10086;
        NotificationManager mNotifyMgr = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();

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

        } catch (Exception e) {
            e.printStackTrace();
        }

        mNotifyMgr.notify(0, notification);
    }

上一篇下一篇

猜你喜欢

热点阅读