gpt4 book ai didi

android - 如何以编程方式关闭/取消android中的状态栏通知

转载 作者:IT老高 更新时间:2023-10-28 22:11:47 29 4
gpt4 key购买 nike

我使用下面给出的带有特定 ID 的代码以编程方式在 android 中创建了一个状态栏通知

    Notification notification;
public static NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;

private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) {
myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis();
notification = new Notification(icon, NotificationTicket, when);
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
myNotificationManager.notify(NOTIFICATION_ID, notification);

}

现在我想要以编程方式在单击应用程序中的按钮时删除此通知。

我怎样才能做到这一点?

最佳答案

你可以用这个:

public void clearNotification() {
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}

关于android - 如何以编程方式关闭/取消android中的状态栏通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835335/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com