package com.yckj.ycsafehelper.utils;import java.lang.reflect.Field;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.os.Build;import android.widget.Toast;public class BadgeUtil {/** * Set badge count * 针对 Samsung / xiaomi / sony 手机有效 *  * @param context *            The context of the application package. * @param count *            Badge count to be set */public static void setBadgeCount(Context context, int count) {if (count <= 0) {count = 0;} else {count = Math.max(0, Math.min(count, 99));}if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {sendToXiaoMi(context, count);} else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {sendToSony(context, count);} else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {sendToSamsumg(context, count);} else {Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();}}/** * 向小米手机发送未读消息数广播 *  * @param count */private static void sendToXiaoMi(Context context, int count) {try {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) {e.printStackTrace();// 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);}}/** * 向索尼手机发送未读消息数广播 * 据说:需添加权限:<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"> [未验证] *  * @param count */private static void sendToSony(Context context, int count) {String launcherClassName = getLauncherClassName(context);if (launcherClassName == null) {return;}boolean isShow = true;if (count == 0) {isShow = false;}Intent localIntent = new Intent();localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);// 是否显示localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",launcherClassName);// 启动页localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE",String.valueOf(count));// 数字localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",context.getPackageName());// 包名context.sendBroadcast(localIntent);}/** * 向三星手机发送未读消息数广播 *  * @param count */private static void sendToSamsumg(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);}/** * 重置、清除Badge未读显示数 *  * @param context */public static void resetBadgeCount(Context context) {setBadgeCount(context, 0);}/** * Retrieve launcher activity name of the application from the context *  * @param context *            The context of the application package. * @return launcher activity name of this application. From the *         "android:name" attribute. */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_DEFAULTif (info == null) {info = packageManager.resolveActivity(intent, 0);}return info.activityInfo.name;}}</uses-permission>

更多相关文章

  1. Android 获取手机信号
  2. Android应用程序键盘(Keyboard)消息处理机制分析(28)
  3. Android 手机震动
  4. Android 手机与Android Studio wifi联调
  5. android 里使用Socket进行发送消息案例
  6. Android Handler消息处理机制面试5连问
  7. Android应用程序键盘(Keyboard)消息处理机制分析(12)

随机推荐

  1. android progressbar 显示两个进度样式的
  2. 解决Android中No resource found that ma
  3. Android(安卓)EditText 多行,滚动条 等
  4. Android(安卓)UI布局中设置了fill_parene
  5. android 接听和挂断实现方式
  6. Android提示版本更新的实现
  7. Android(安卓)Recovery模式
  8. Android平台常见属性集合
  9. Android(安卓)SDK Manager更新不了的解决
  10. Android(安卓)中RelativeLayout各个属性