miui Set App Badge - Android Phone

Android examples for Phone:Badge

Description

miui Set App Badge

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import android.util.Log;

public class Main {
    private static final String tag = "BadgeUtil";

    public static void miuiSetAppBadge(Context context, int num) {

        Intent localIntent = new Intent(
                "android.intent.action.APPLICATION_MESSAGE_UPDATE");
        localIntent.putExtra(/* ww w.j a va  2 s . c  om*/
                "android.intent.extra.update_application_component_name",
                context.getPackageName() + "/"
                        + getLaunchActivityName(context));
        if (num > 0) {
            localIntent.putExtra(
                    "android.intent.extra.update_application_message_text",
                    String.valueOf(Math.min(num, 99)));
        } else {
            localIntent.putExtra(
                    "android.intent.extra.update_application_message_text",
                    "");
        }
        context.sendBroadcast(localIntent);
    }

    private static String getLaunchActivityName(Context context) {
        PackageManager localPackageManager = context.getPackageManager();
        Intent localIntent = new Intent("android.intent.action.MAIN");
        localIntent.addCategory("android.intent.category.LAUNCHER");
        for (ResolveInfo localResolveInfo : localPackageManager
                .queryIntentActivities(localIntent, 0)) {
            if (!localResolveInfo.activityInfo.applicationInfo.packageName
                    .equalsIgnoreCase(context.getPackageName())) {
                continue;
            }
            return localResolveInfo.activityInfo.name;
        }
        return null;
    }
}

Related Tutorials