android - 单击其中一个按钮(不是内容区域)后如何关闭大 View 通知

标签 android notifications android-notifications

我已经按照 Google 的教程创建了一个 Big View Styled Notification (此处 their source code ):

screenshot

my own simple test app单击其中一个按钮后显示 toast (在应用程序中或在大通知中):

screenshot

除一个小问题外,它运行良好:

单击通知中的其中一个按钮(“打开”或“闪烁”)时 - 我看不到 toast ,因为它被黑色通知 Pane 覆盖。

所以我的问题是:如何在我单击其中一个按钮后取消大通知并关闭通知 Pane ?(即不是通知内容,而是选择按钮)。

这是我在 MainActivity.java 中的通知构建代码- 那里有一个 setAutoCancel(true) 调用,但它只适用于内容区域:

Intent openIntent = new Intent(this, RegionService.class);
openIntent.setAction("open");
PendingIntent piOpen = PendingIntent.getService(this, 0, openIntent, 0);

Intent flashIntent = new Intent(this, RegionService.class);
flashIntent.setAction("flash");
PendingIntent piFlash = PendingIntent.getService(this, 0, flashIntent, 0);

mNotificationBuilder = new NotificationCompat.Builder(mContext)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(question)
    .setContentText(question)
    .setTicker(question)
    .setContentIntent(contentIntent)
    .setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_ALL) 
    .setStyle(new NotificationCompat.BigTextStyle()
         .bigText(question))
    .addAction(R.drawable.open,
        getString(R.string.open_car_short), 
        piOpen)
    .addAction(R.drawable.flash,
        getString(R.string.flash_lights_short), 
        piFlash);

另外,我想知道为什么字符串“OPEN”和“FLASH”要大写?

更新:我试过cancel(NOTIFICATION_ID)正如 Marcin 所建议的那样(谢谢),它会关闭通知,但不会关闭通知 Pane 。所以还是看不到toasts

最佳答案

您需要使用 NotificationManager 的 cancel() 从代码中取消此通知:http://developer.android.com/reference/android/app/NotificationManager.html

关于android - 单击其中一个按钮(不是内容区域)后如何关闭大 View 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360076/

相关文章:

php - 结果无法转换为 JSON 数组

actionscript-3 - Adobe Air (AS3) 可以直接使用 socket.io 吗?

android对话框在它后面打开另一个 View

java - android.app.RemoteServiceException : at android. app.ActivityThread$H.handleMessage 发送通知时

android - 在android通知中动态更改图标

Android 禁用 WebView 触摸但保持缩放/滚动

android - 从最近和历史中排除 Activity

android - Jetpack Compose 自定义徽章与 CircleShape 的动态文本大小未正确绘制

ios - 如果在初始化期间发布通知,则未收到通知?

android - 当我收到通知时,我的应用程序在后台打开时只显示通知而不打开应用程序?