当android应用程序在android上打开时,如何重置徽章计数

当android应用程序在android上打开时,如何重置徽章计数,android,Android,打开应用程序后,我希望删除徽章计数。现在,只有当我通过锁屏上的推送消息打开应用程序时,它才会被删除。推送消息随后从锁屏上删除,因此我也可以将这个问题表述为“如何从锁屏上删除推送消息” 使用此代码,我可以检索通知: @Override protected void onStart() { super.onStart(); NotificationManager notificationManager = (NotificationManager) getSystemService(

打开应用程序后,我希望删除徽章计数。现在,只有当我通过锁屏上的推送消息打开应用程序时,它才会被删除。推送消息随后从锁屏上删除,因此我也可以将这个问题表述为“如何从锁屏上删除推送消息”

使用此代码,我可以检索通知:

@Override
protected void onStart() {
    super.onStart();
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
        for (StatusBarNotification statusBarNotification : notifications) {
            statusBarNotification.getNotification().number = 0;
        }
    }
}

但将数字设置为0时不会发生任何情况。

正确答案由@Alex Kamenkov提交

   @Override
    protected void onStart() {
        super.onStart();
        resetBadgeCounterOfPushMessages();
    }

    private void resetBadgeCounterOfPushMessages() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
    }

@Alex Kamenkov提交了正确答案

   @Override
    protected void onStart() {
        super.onStart();
        resetBadgeCounterOfPushMessages();
    }

    private void resetBadgeCounterOfPushMessages() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
    }

清除通知以及重置标记计数:NotificationManager nMgr=(NotificationManager)getSystemService(Context.notification\u服务);如果(nMgr!=null){nMgr.cancelAll();}清除通知并重置标记计数:NotificationManager nMgr=(NotificationManager)getSystemService(Context.notification_服务);如果(nMgr!=null){nMgr.cancelAll();}