Desktop icon shows unread count

public class BadgeUtil {

    public static void applyBadgeCount(Context context, int badgeCount) {
        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            // 判断机型是否是小米
context.startService(new Intent(context, BadgeIntentService.class).putExtra("badgeCount", badgeCount));
} else {
            ShortcutBadger.applyCount(context, badgeCount);
}
    }

    public static void removeBadgeCount(Context context) {
        ShortcutBadger.removeCount(context);
}


}


public class BadgeIntentService extends IntentService {

    private int notificationId = 0;
    public BadgeIntentService() {
        super("BadgeIntentService");
}


    private NotificationManager mNotificationManager;
@Override
public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}


    @Override
protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            int badgeCount = intent.getIntExtra("badgeCount", 0);
mNotificationManager.cancel(notificationId);
notificationId++;
Notification.Builder builder = new Notification.Builder(getApplicationContext())


                    .setContentTitle("")
                    .setContentText("")
                    .setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount);
mNotificationManager.notify(notificationId, notification);
}
    }
}


public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BadgeUtil.applyBadgeCount(MainActivity.this,3);
}
    @Override
protected void onDestroy() {
        super.onDestroy();
BadgeUtil.removeBadgeCount(MainActivity.this);
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326175533&siteId=291194637