Android 仿QQ桌面icon显示未读信息数量

 三星已做测试OK,其他没有手机没做做对应的测试。Android <wbr>仿QQ桌面icon显示未读信息数量


//Currently, it's working from Android 4.0. 
//But some devices, which are released from the manufacturers, are not working.


public class BadgeUtil {
public enum Platform{
samsung,lg,htc,mi,sony
};


    private static final String TAG = "BadgeUtil";


    public static void setBadgeCount(Context context, int count,Platform platform) {
    Intent badgeIntent = null;
    if(platform.equals(Platform.samsung)){
    Log.e(TAG, "samsung....");
    badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
       badgeIntent.putExtra("badge_count", count);
       badgeIntent.putExtra("badge_count_package_name", context.getPackageName());
       badgeIntent.putExtra("badge_count_class_name", getLauncherClassName(context));
    }
   
   
    if(platform.equals(Platform.mi)){
    Log.e(TAG, "xiaoMiShortCut....");
    badgeIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
badgeIntent.putExtra("android.intent.extra.update_application_component_name",getLauncherClassName(context));
    badgeIntent.putExtra("android.intent.extra.update_application_message_text", count);
    context.sendBroadcast(badgeIntent);
    }
   
    if(platform.equals(Platform.sony)){
    Log.e(TAG, "sony....");
    badgeIntent = new Intent();
    badgeIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    badgeIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    badgeIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", getLauncherClassName(context));
    badgeIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", count);
    badgeIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
    }
    if(platform.equals(Platform.htc)){
    Log.e(TAG, "htc....");
    badgeIntent = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");
    badgeIntent.putExtra("packagename", getLauncherClassName(context));
    badgeIntent.putExtra("count", count);
    }
   
    if(platform.equals(Platform.lg)){
    Log.e(TAG, "lg....");
    badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    badgeIntent.putExtra("badge_count_package_name", context.getPackageName());
    badgeIntent.putExtra("badge_count_class_name", getLauncherClassName(context));
    badgeIntent.putExtra("badge_count", count);
    }
        context.sendBroadcast(badgeIntent);
    }


  
    public static void resetBadgeCount(Context context,Platform platform) {
        setBadgeCount(context, 0,platform);
    }


   
    private static String getLauncherClassName(Context context) {
        PackageManager packageManager = context.getPackageManager();


        Intent intent = new Intent(Intent.ACTION_MAIN);
        // To limit the components this Intent will resolve to, by setting an
        // explicit package name.
        intent.setPackage(context.getPackageName());
        intent.addCategory(Intent.CATEGORY_LAUNCHER);


        // All Application must have 1 Activity at least.
        // Launcher activity must be found!
        ResolveInfo info = packageManager
                .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);


        // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
        // if there is no Activity which has filtered by CATEGORY_DEFAULT
        if (info == null) {
            info = packageManager.resolveActivity(intent, 0);
        }
        Log.e("getLauncherClassName ", "name = "+ info.activityInfo.name);
        return info.activityInfo.name;
    }
}






在Activity中设置Button的点击事件:




findViewById(R.id.btn_badge).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "厂商:"+Build.MANUFACTURER, 0).show();
int count;
try {
count = Integer.valueOf(etNum.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "请输入整数", 0).show();
return;
}
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
     BadgeUtil.setBadgeCount(MainActivity.this, count, BadgeUtil.Platform.mi);
   }else if(Build.MANUFACTURER.equalsIgnoreCase("samsung")){
    BadgeUtil.setBadgeCount(MainActivity.this, count, BadgeUtil.Platform.samsung);
   }else if(Build.MANUFACTURER.equalsIgnoreCase("htc")){
    BadgeUtil.setBadgeCount(MainActivity.this, count, BadgeUtil.Platform.htc);
   }else if(Build.MANUFACTURER.equalsIgnoreCase("lg")){
    BadgeUtil.setBadgeCount(MainActivity.this, count, BadgeUtil.Platform.lg);
   }else if(Build.MANUFACTURER.equalsIgnoreCase("sony")){
    BadgeUtil.setBadgeCount(MainActivity.this, count, BadgeUtil.Platform.sony);
   }
}

});



转自:http://blog.sina.com.cn/s/blog_96a146890102v9mv.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值