android实现桌面icon未读信息图标

package com.example.headpull;

import java.util.List;

import android.content.Context;  
import android.content.Intent;  
import android.content.pm.PackageManager;  
import android.content.pm.ResolveInfo;  
  
//BadgeUtil provides static utility methods to set "badge count" on Launcher (by Samsung, LG).   
//Currently, it's working from Android 4.0.   
//But some devices, which are released from the manufacturers, are not working.  
  
public class BadgeUtil {  
  
	  public static void setBadge(Context context, int count) {
	        setBadgeSamsung(context, count);
	        setBadgeSony(context, count);
	    }

	    public static void clearBadge(Context context) {
	        setBadgeSamsung(context, 0);
	        clearBadgeSony(context);
	    }


	    public static void setBadgeSamsung(Context context, int count) {
	        String launcherClassName = getLauncherClassName(context);
	        if (launcherClassName == null) {
	            return;
	        }
	        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);
	    }

	    private static void setBadgeSony(Context context, int count) {
	        String launcherClassName = getLauncherClassName(context);
	        if (launcherClassName == null) {
	            return;
	        }

	        Intent intent = new Intent();
	        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

	        context.sendBroadcast(intent);
	    }


	    private static void clearBadgeSony(Context context) {
	        String launcherClassName = getLauncherClassName(context);
	        if (launcherClassName == null) {
	            return;
	        }

	        Intent intent = new Intent();
	        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
	        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

	        context.sendBroadcast(intent);
	    }

	    private 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;
	    }
} 

猜你喜欢

转载自274137570-qq-com.iteye.com/blog/2260869