【问题标题】:Android Notification with Actions and a DropDown Menu带有操作和下拉菜单的 Android 通知
【发布时间】:2018-09-17 20:55:57
【问题描述】:

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

如果您点击时钟,则会打开以下下拉菜单:

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

有人知道如何实现吗?

【问题讨论】:

  • 这家伙的回答会救你stackoverflow.com/a/21927248/6751183
  • 好吧,我不想用按钮发出通知,我想在点击通知操作后展开我的普通 android 通知!
  • @VIGOPIXelInteractiveInc 我不想使用服装内容视图发出通知,通知应该只在通知操作单击时展开(有点像回复功能,其中文本视图出现在通知中.. .
  • @the_dani 你的意思是你想要通知,就像在带有按钮、文本和图像的音乐播放器通知中一样?
  • @KevinKurien 我有一个带有一些操作的正常通知。当用户单击“贪睡”操作按钮时,通知应该展开,显示一个带有一些可能贪睡延迟的 RadioButton 列表……我想我在谷歌应用中看到过,但我不记得是哪一个了!

标签: android android-notifications notification-action


【解决方案1】:

这是标准的,适用于来自 Android oreo 的所有通知。

对于所有通知,您都可以选择打盹。如果你想为旧的Android设备实现同样的ui,你可以直接查看Android oreo System 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

【讨论】:

  • 嗯,这很清楚,但是有没有办法让如上所示的通知(对话框)可以通过通知操作访问并检索其值?
【解决方案2】:

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?
  • “通知操作”是什么意思?
  • 正如我的问题中所述,“下拉列表”应通过单击通知操作“打盹”来显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多