send Badge To Xiao Mi - Android Hardware

Android examples for Hardware:Vendor

Description

send Badge To Xiao Mi

Demo Code


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class Main{
    private static void sendBadgeToXiaoMi(int count) {
        Context context = App.getInstance();

        try {//from www  .  jav  a  2 s. c  o m
            Class miuiNotificationClass = Class
                    .forName("android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField(
                    "messageCount");
            field.setAccessible(true);
            field.set(miuiNotification,
                    String.valueOf(count == 0 ? "" : count)); // ???????-->???????????miui 6???
        } catch (Exception e) {
            // miui 6??????
            Intent localIntent = new Intent(
                    "android.intent.action.APPLICATION_MESSAGE_UPDATE");
            localIntent
                    .putExtra(
                            "android.intent.extra.update_application_component_name",
                            context.getPackageName() + "/"
                                    + getLauncherClassName(context));
            localIntent.putExtra(
                    "android.intent.extra.update_application_message_text",
                    String.valueOf(count == 0 ? "" : count));
            context.sendBroadcast(localIntent);
        }
    }
    public static String getLauncherClassName(Context context) {

        PackageManager pm = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = pm
                .queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                String className = resolveInfo.activityInfo.name;
                return className;
            }
        }
        return null;
    }
}

Related Tutorials