【问题标题】:Create custom notification including Edit text android创建自定义通知,包括编辑文本android
【发布时间】:2015-10-04 22:33:41
【问题描述】:

我想创建一个自定义通知来直接从通知中回复短信,如下所示:

据我了解,正常通知的高度必须为 64dp,但您可以使用 API >16 中较大的通知作为可扩展通知,但我认为 64dp 高度适合我的情况。我使用了这段代码,但是当我的自定义通知布局有编辑文本时它会崩溃:

RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.widget);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContent(remoteViews);
                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(100, mBuilder.build());

错误:

android.app.RemoteServiceException: Bad notification posted from package com.example.x: Couldn't expand RemoteViews for: StatusBarNotification

我该怎么办?

【问题讨论】:

  • "创建自定义通知,包括 Edit text android" -- 你不能将 EditText 放在 RemoteViews 中。
  • 为什么?我应该怎么做才能发出自定义通知?

标签: android notifications custom-controls


【解决方案1】:

创建自定义通知,包括编辑文本 android

您不能将EditText 小部件放入Notification、应用小部件或任何其他使用RemoteViews 的东西中。

为什么?

因为RemoteViews 就是这样写的。您仅限于certain widgets and containers

我应该怎么做才能发出自定义通知?

重新设计它以不涉及EditText

更新:在 Android 7.0+ 上,您可以使用 MessagingStyleRemoteInput 来接受来自 Notification 的用户输入。这不符合问题的要求,但它是最接近的选项。

【讨论】:

  • 还有其他方法(不是远程视图)来创建自定义通知吗?
  • @Mohammad:不适用于应用程序。
  • @JakeWilson801:是的,这是 Android 7.0+ 设备的一个选项。
  • 我在简历上注明@CommonsWare 回复了我的评论。
  • 推荐更多答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 2023-03-23
  • 1970-01-01
  • 2014-02-09
相关资源
最近更新 更多