带有操作和下拉菜单的Android通知

10

Android的Google应用有一个通知,例如显示当地天气。如果您在通知上向左滑动,则会显示如下所示的“时钟图标”操作(例如,延迟提醒)。

Google Notification with Weather Forecast

如果您单击时钟,将打开以下下拉菜单:

Notification with dropdown menu

这是Android系统的一项功能,我想在我的应用中实现它。它应该通过通知操作打开,我想设置自定义选项,并需要获取所选值。

有人知道如何实现吗?


这个人的回答会帮到你 https://dev59.com/fWEh5IYBdhLWcg3w3muK#21927248 - Vincent Mungai
我不想用按钮来创建通知,我想在通知操作被点击后扩展我的普通Android通知! - the_dani
@VIGOPIXelInteractiveInc 我不想使用自定义内容视图创建通知,通知应该只在通知操作单击时展开(有点像回复功能,在通知中出现文本视图...)。 - the_dani
请使用以下链接 https://developer.android.com/training/notify-user/custom-notification您需要的是大型通知内容。 - Pouya Danesh
@pouya 但是我需要在通知操作被按下后调用我的自定义布局。如果我使用可展开的布局,这是不可能的吗? - the_dani
显示剩余3条评论
2个回答

0

Android系统提供了扩展功能,用于不同的通知样式。您需要一个显示用户可以选择的选项列表的视图。因此,您需要创建一个自定义视图并使用您的选项填充它。

您可以使用Android通知系统提供的Notification.DecoratedCustomViewStyle()设置自定义视图。

如果您想要折叠和展开视图的不同外观,则可以使用以下方法进行设置 -

setStyle(new Notification.DecoratedCustomViewStyle())
   .setCustomContentView(remoteViews)
   .setCustomBigContentView(bigRemoteView);

您需要为布局中指定的所有选项添加不同的待处理意图。

例如 -

RemoteViews firstOption = ....;
Intent firstOptionIntent = // add some argument in this intent which depicts this option
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,   flags);
setOnClickPendingIntent(R.id.<option_layout_id>, pendingIntent);

// 对于其他选项同样适用

RemoteViews secondOption = ....;

要在通知操作单击时添加下拉列表,您需要使用2个不同的布局,一个用于折叠视图,另一个用于展开视图 -

Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout) // collapsed view layout
    .setCustomBigContentView(notificationLayoutExpanded) // expanded view layout
    .build();

通过这个解决方案,我可以实现那种转换效果并最终实现一个旋转器吗? - the_dani
能否仅通过通知操作打开Big ContentView? - the_dani
正如我在问题中所描述的那样,“下拉列表”应该通过单击通知操作“推迟”来显示。 - the_dani
用户可以通过下拉来打开扩展视图(带有下拉列表)...这是不应该的。 - the_dani
如果您的问题是如何在以前的通知中单击操作按钮后使用扩展视图更新通知布局,那么您可以这样做。 - Vishal Arora
显示剩余3条评论

0

这是 Android Oreo 中所有通知的标准设置。

对于所有通知,您都可以选择将其暂停。如果您想要在老旧的 Android 设备上实现相同的 UI,您可以直接查看 Android Oreo 系统 UI 的源代码,从中获取 Android 8 及以上版本中的暂停选项。

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java#274

检查此字符串的使用

<!-- Notification: Snooze panel: message indicating how long the notification was snoozed for. [CHAR LIMIT=100]-->
1520    <string name="snoozed_for_time">Snoozed for <xliff:g id="time_amount" example="15 minutes">%1$s</xliff:g></string>

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/res/values/strings.xml#1519


这很清楚,但是否有一种方法可以通过通知操作使像上面显示的通知(对话框)可访问并检索其值? - the_dani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接