【问题标题】:Android Notifications - Action button on normal view notification (without expanding to big view)Android 通知 - 普通视图通知上的操作按钮(不扩展到大视图)
【发布时间】:2016-07-26 08:51:55
【问题描述】:

我正在尝试为我的应用程序创建一个通知,该通知具有通常的图标、标题和子文本,但也有通知右侧的操作按钮,而无需展开到大模式。文本行总是很短,所以按钮有足够的空间。目前看起来是这样的

1

但是当它被另一个通知压缩到正常视图时,它看起来像这样并且失去了按钮

2

我已经使用 RemoteViews 制作了一个自定义视图,但问题是我不想手动设置图标和文本的位置和样式。在多个设备上让系统继续照顾它们会更加可靠。

是否可以将我自己的按钮添加到通知在第二张图片中的显示方式的右侧?有点像将远程视图的一个元素与 Android 生成的标准布局相结合?根据我的阅读,我认为不可能只在普通视图中使用操作按钮?

我希望能够这样做的原因是允许用户点击警报以确认他们已经看到它,这样它就不会响起,但他们仍然有即将发生的事件的通知。点击闹钟按钮以外的任何地方都应该像平常一样打开应用程序。

代码方面,它看起来像这样,远程视图被注释掉了。

    RemoteViews remoteViews = new RemoteViews(activity.getPackageName(),
                R.layout.custom_notification);

        String eventDescription = preferences.getString(activity.getResources().getString(R.string.next_event_description), "Freemasons run the country");
        String eventTime = preferences.getString(activity.getResources().getString(R.string.next_event_time), "00:00");

    NotificationManager notificationManager = (NotificationManager)
    activity.getSystemService(activity.NOTIFICATION_SERVICE);

    Intent intent = new Intent(activity, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(activity, 0 , intent, 0);

    Notification.Action action = new Notification.Action.Builder(
            Icon.createWithResource(activity, R.mipmap.alarm_icon),
            nextEventAlarmMinutes.toString(),
            pIntent).build();

    Notification.Builder builder  = new Notification.Builder(activity)
            //.setContent(remoteViews)
            .setContentTitle(eventDescription)
            .setContentText(eventTime)
            .setSmallIcon(R.mipmap.alarm_icon)
            .setContentIntent(pIntent)
            .addAction(action)
            //.setPriority(Notification.PRIORITY_HIGH)
            .setWhen(0)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setAutoCancel(false);

        notificationManager.notify(0, builder.build());

自定义视图xml只有一个相对布局和一个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="10dp" />

是否有可能是相对布局覆盖了整个内容,我可以只使用按钮作为自己的视图,而没有父视图持有它?

【问题讨论】:

  • 嵌入有问题的图片,而不是使用网址
  • @Matt 能否请您发布部分相关代码?

标签: android notifications


【解决方案1】:

即使通知已折叠,您也可以使用带有媒体控件的通知来显示操作按钮。参见文档here.

【讨论】:

    【解决方案2】:
    //Share intent
    Intent shareAction = new Intent(this, ActShare.class);
    PendingIntent pendingIntentShare = PendingIntent.getActivity(this, 12345, shareAction, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.share, "Share", pendingIntentShare );
    
    //Editintent
    Intent EditAction = new Intent(this, ActEdit.class);
    PendingIntent pendingIntentEdit = PendingIntent.getActivity(this, 12345, EditAction, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.edit, "Edit", pendingIntentEdit);
    
    //Share intent
    Intent deleteAction = new Intent(this, ActDelete.class);
    PendingIntent pendingIntentDelete = PendingIntent.getActivity(this, 12345, deleteAction, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.addAction(R.drawable.delete, "Delete", pendingIntentDelete);
    

    将此代码添加到您的默认 android 通知中的 addAction..

    【讨论】:

    • 抱歉,我根本不明白这是如何回答这个问题的?这不仅会在通知中添加三个不同的操作,而且还需要它处于大模式吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多