美文网首页
Android角标未读数显示问题

Android角标未读数显示问题

作者: 背锅TV丶伴奏大师 | 来源:发表于2021-12-27 15:46 被阅读0次

    第一步:

    package com.netease.nim.uikit.common.framework.infra;
    
    import android.content.Context;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.text.TextUtils;
    
    import java.util.HashMap;
    
    public final class Handlers {
        public static final String DEFAULT_TAG = "Default";
    
        private static Handlers instance;
    
        public static synchronized Handlers sharedInstance() {
            if (instance == null) {
                instance = new Handlers();
            }
    
            return instance;
        }
    
        private static Handler sharedHandler;
    
        /**
         * get shared handler for main looper
         *
         * @param context
         * @return
         */
        public static final Handler sharedHandler(Context context) {
            /**
             * duplicate handlers !!! i don't care
             */
    
            if (sharedHandler == null) {
                sharedHandler = new Handler(context.getMainLooper());
            }
    
            return sharedHandler;
        }
    
        /**
         * get new handler for main looper
         *
         * @param context
         * @return
         */
        public static final Handler newHandler(Context context) {
            return new Handler(context.getMainLooper());
        }
    
        private Handlers() {
    
        }
    
        /**
         * get new handler for a background default looper
         *
         * @return
         */
        public final Handler newHandler() {
            return newHandler(DEFAULT_TAG);
        }
    
        /**
         * get new handler for a background stand alone looper identified by tag
         *
         * @param tag
         * @return
         */
        public final Handler newHandler(String tag) {
            return new Handler(getHandlerThread(tag).getLooper());
        }
    
        private final HashMap<String, HandlerThread> threads = new HashMap<String, HandlerThread>();
    
        private final HandlerThread getHandlerThread(String tag) {
            HandlerThread handlerThread = null;
    
            synchronized (threads) {
                handlerThread = threads.get(tag);
    
                if (handlerThread == null) {
                    handlerThread = new HandlerThread(nameOfTag(tag));
    
                    handlerThread.start();
    
                    threads.put(tag, handlerThread);
                }
            }
    
            return handlerThread;
        }
    
        private final static String nameOfTag(String tag) {
            return "HT-" + (TextUtils.isEmpty(tag) ? DEFAULT_TAG : tag);
        }
    }
    
    

    第二步:

    api "me.leolin:ShortcutBadger:1.1.22@aar"
    

    第三步:

    package com.netease.nim.uikit.common.badger;
    
    import android.os.Handler;
    import android.util.Log;
    
    import com.netease.nim.uikit.common.framework.infra.Handlers;
    
    import me.leolin.shortcutbadger.ShortcutBadger;
    
    /**
     * APP图标未读数红点更新接口
     * https://github.com/leolin310148/ShortcutBadger
     * <p>
     * Created by huangjun on 2017/7/25.
     */
    
    public class Badger {
    
        private static final String TAG = "Badger";
    
        private static Handler handler;
    
        private static boolean support = true;
    
        public static void updateBadgerCount(final int unreadCount,Context context) {
            if (!support) {
                return; // O版本及以上不再支持
            }
            if (handler == null) {
                handler = Handlers.sharedInstance().newHandler("Badger");
            }
            handler.removeCallbacksAndMessages(null);
            handler.postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    int badgerCount = unreadCount;
                    if (badgerCount < 0) {
                        badgerCount = 0;
                    } else if (badgerCount > 99) {
                        badgerCount = 99;
                    }
                    boolean res = ShortcutBadger.applyCount(context, badgerCount);
                    if (!res) {
                        support = false; // 如果失败就不要再使用了!
                    }
                    Log.i(TAG, "update badger count " + (res ? "success" : "failed"));
                }
            }, 200);
        }
    }
    

    第四步:在需要的地方使用

    Badger.updateBadgerCount(unreadNum);
    

    目前:
    1.oppo不开放数字角标的功能。

    1. vivo之前是不开放角标的,现在官方文档里开放了角标并且提供了对应的方法,与华为用法差不多,不过存在一定的限制条件。一、不能使用vivo自身的推送渠道,二、手机需要8.0及以上的Android版本。
      3.小米提前把手机的通知过滤给关掉,不然只会有通知不会有角标显示。

    相关文章

      网友评论

          本文标题:Android角标未读数显示问题

          本文链接:https://www.haomeiwen.com/subject/vmuyqrtx.html