簡體   English   中英

如何從狀態欄中刪除通知?

[英]How do I remove Notification from status bar?

我會根據自己的狀況在狀態欄中顯示Notification 到此為止,還可以。

現在我的應用程序中的問題是當我返回到應用程序時, Notification仍顯示在狀態欄中。

從應用程序回來時,我不希望收到Notification 為此,請給我一些建議。

我沒有足夠的代表來添加評論,所以@Commonware。 如果您提供了更具體的詳細信息,例如告訴他們在創建通知時使用“通知ID”並使用該ID取消通知,則對將來的讀者會更有用。 如果您給出答案,請使其完整或至少提供完整的詳細信息。 這些帖子不僅用於OP,還可以幫助遇到此帖子的其他人。 像這樣

final NotificationManager notificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

然后取消它,你打電話

notificationManager.cancel(NOTIFICATION_ID);

或者你可以打電話

notificationManager.cancelAll();
notificationManager.cancelAll();

您可以cancel()你自己的Notifications通過NotificationManager 您可以決定何時cancel()和何時顯示它。

private void cancelNotificationInBar() {
        NotificationCreator notification = new NotificationCreator(getApplicationContext());
        notification.mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        if (notification.mNotificationManager != null) {
            notification.mNotificationManager.cancelAll();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM