Put a badge on the icon

Put a badge on the icon

I want to put a badge on the icon displayed on HOME like the iPhone.

I dealt with it at work, so I will write it as a memo.

There is a method, but please note that it is not applicable to all HOME apps and can only be displayed on some HOME apps.

Applicable HOME app

After checking, it worked with the following HOME app.

--Galaxy TouchWiz

Other HOME apps have not been verified yet, so I will verify them later. At work, I also displayed it on Xperia Home, but in that case the implementation is a little different, so I will add it later. ..

Display method

Then it is an implementation. It's a hassle, so I'll just paste the roughly created methods.

Please refer to here. (Rather, it is almost the same.)

    /**
     *Clear the badge on the icon.<br>
     ** Set 0
     * 
     * @param context context
     */
    public static void clearIconBadge(final Context context) {
        setIconBadge(context, 0);
    }

    /**
     *Put a badge on the icon.
     *
     * @param context context
     * @param count Badge number to display
     */
    public static void setIconBadge(final Context context, final int count) {
        if (context == null) {
            return;
        }
        final String launcherClassName = getLauncherClassName(context);
        if (TextUtils.isEmpty(launcherClassName)) {
            return;
        }
        final Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", count);
        intent.putExtra("badge_count_package_name", context.getPackageName());
        intent.putExtra("badge_count_class_name", launcherClassName);

        context.sendBroadcast(intent);
    }

    /**
     *Get the class name of the running app.
     *
     * @param context Context.
     * @return Class name of the running app<br>
     *Null if it cannot be obtained normally.
     */
    @Nullable
    private static String getLauncherClassName(@NonNull final Context context) {
        final PackageManager pm = context.getPackageManager();
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(intent, 0);

        for (final ResolveInfo resolveInfo : resolveInfoList) {
            if (resolveInfo == null) {
                continue;
            }
            final String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                return resolveInfo.activityInfo.name;
            }
        }

        return null;
    }

Call setIconBadge () when you want the badge to appear. Also, if you want to erase the badge, you can set 0, so if it is the code written in ↑ It's OK if you call clearIconBadge ().

Write in Kotlin

I will post it for the Kotlin language as well. Paste the created Utility as it is.

class Utility {
    companion object {

        /**
         *Clear the icon badge.<br>
         ** Set 0
         * 
         * @param context context
         */
        fun clearIconBadge(context: Context) {
            setIconBadge(context, 0)
        }

        /**
         *Put a badge on the icon.

         * @param context context
         *
         * @param count Badge number to display
         */
        fun setIconBadge(context: Context?, count: Int) {
            if (context == null) {
                return
            }
            val launcherClassName = getLauncherClassName(context)
            if (TextUtils.isEmpty(launcherClassName)) {
                return
            }
            val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
            intent.putExtra("badge_count", count)
            intent.putExtra("badge_count_package_name", context.packageName)
            intent.putExtra("badge_count_class_name", launcherClassName)

            context.sendBroadcast(intent)
        }

        /**
         *Get the class name of the running app.
         * 
         * @param context Context.
         * @return Class name of the running app<br></br>
         * *Null if it cannot be obtained normally.
         */
        private fun getLauncherClassName(context: Context): String? {
            val pm = context.packageManager
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            val resolveInfoList = pm.queryIntentActivities(intent, 0)

            for (resolveInfo in resolveInfoList) {
                if (resolveInfo == null) {
                    continue
                }
                val pkgName = resolveInfo.activityInfo.applicationInfo.packageName
                if (pkgName.equals(context.packageName, ignoreCase = true)) {
                    return resolveInfo.activityInfo.name
                }
            }

            return null
        }
    }
}

Recommended Posts

Put a badge on the icon
[Rails] How to put a crown mark on the ranking function
A note on the libGDX Utils class
A rudimentary note on the Fibonacci sequence
Come out with a suffix on the method
Come out with a suffix on the method 2
Put JetBrains on Ubuntu
[Rails] Put the same restrictions on multiple pieces of information
[Swift 5] Select a date with the IDate Picker on iOS14
A note about the seed function of Ruby on Rails
I put the Quarkus app on GKE (Google Kubernetes Engine).
Try launching a webAP server on the micro using Helidon
Use the high-performance SQL development tool "A5: SQL Mk-2" on Ubuntu
I want to put the JDK on my Mac PC
Why put a line break at the end of the file
Launch a terminal on VScode
Put Ubuntu 20.04.1 on Raspberry Pi 4
A note about the scope
Scala runs on the JVM
Put Oracle Java 8 on CircleCI 2
Post a video on rails
Logic to draw a circle with ASCII art on the console
(Ruby on Rails6) Create a function to edit the posted content