android 自定义通知布局中的编辑文本?

android edittext in a custom notification layout?

想法是在自定义通知布局中显示一个 phone 号码,如果 phone 号码正常,则用户接受 phone 号码并启动待定意图那个 phone 号码。否则,用户可以选择编辑同一通知上的 phone 号码,并在号码正确后接受。是否可以使用自定义通知布局来做到这一点?哪些 API 版本支持此类通知?

@Karakuri 是对的。但是Android宣布了新版本AndroidN。AndroidN支持内联回复。

添加内联操作。

  1. 创建一个 RemoteInput.Builder 的实例,您可以将其添加到您的通知操作中。此 class 的构造函数接受系统用作文本输入键的字符串。稍后,您的手持应用程序会使用该键来检索输入的文本。
// Key for the string that's delivered in the action's intent.
private static final String KEY_TEXT_REPLY = "key_text_reply";
String replyLabel = getResources().getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
        .setLabel(replyLabel)
        .build();
  1. 使用 addRemoteInput() 将 RemoteInput 对象附加到操作。
// Create the reply action and add the remote input.
Notification.Action action =
        new Notification.Action.Builder(R.drawable.ic_reply_icon,
                getString(R.string.label), replyPendingIntent)
                .addRemoteInput(remoteInput)
                .build();
  1. 将操作应用于通知并发出通知。
// Build the notification and add the action.
Notification newMessageNotification =
        new Notification.Builder(mContext)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle(getString(R.string.title))
                .setContentText(getString(R.string.content))
                .addAction(action))
                .build();

// Issue the notification.
NotificationManager notificationManager =
        NotificationManager.from(mContext);
notificationManager.notify(notificationId, newMessageNotification);

从内联回复中获取用户输入,访问官方文档: https://developer.android.com/preview/features/notification-updates.html

向后兼容性:

Both notification groups and remote input have been a part of the Notification API since Android 5.0 (API level 21) to support Android Wear devices. If you've already built notifications with these APIs, the only action you must take is to verify that the app behavior corresponds to the guidelines described above, and to consider implementing setRemoteInputHistory().

In order to support backward compatibility, the same APIs are available with the support library's NotificationCompat class, allowing you to build notifications that works on earlier Android versions. On handhelds and tablets, users only see the summary notification, so an app should still have an inbox style or an equivalent notification representative for the whole information content of the group. As Android Wear devices allow users to see all child notifications even on older platform levels, you should build child notifications regardless of API level.