【发布时间】:2014-08-13 17:50:02
【问题描述】:
是否可以在statusbar 的扩展通知中获取文本?这意味着将文本设置为我的Notification.BitTextStyle.bigText(),例如在来自 Gmail 的电子邮件通知中。
我想使用 Notification.extras 来获取它,但似乎没有常量,例如 Notification.EXTRA_BIG_TEXT 允许它工作。
还有其他获取此文本的方法吗? 此外,当我使用
String bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;
它返回null,知道为什么吗?我用它来获取电子邮件的主题(Testing the new Gmail notification shortcuts -> 请参阅下面的链接)。
下面是一个扩展的 Gmail 通知示例:(我想得到以下文本 ->
您现在可以在...内获得回复和存档的快捷按钮...
Picture of Gmail Notification (不能发布声望低于10的图片,抱歉)
作为替代方案,我还尝试了以下方法:
RemoteViews rv = mNotification.contentView;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup localView = (ViewGroup) inflater.inflate(rv.getLayoutId(), null);
rv.reapply(getApplicationContext(), localView);
everything = new StringBuilder(150);
for(int i=0; i<((ViewGroup)localView).getChildCount(); ++i) {
View nextChild = ((ViewGroup)localView).getChildAt(i);
try{
TextView tv = (TextView) localView.findViewById(nextChild.getId());
everything.append(tv.getText().toString());
}catch(Exception e){
continue;
}
}
Log.i("abc", everything.toString());
但这对我也不起作用,似乎从未从任何视图中获得任何文本。
更新:
当我使用以下代码从通知中获取文本时...:
body = mExtras.getString(Notification.EXTRA_TEXT);
...只要不是 Gmail/电子邮件通知,一切都可以正常工作:
收到 Gmail 通知后,我会立即收到以下错误消息:
08-19 20:19:37.379: W/Bundle(6646): Key android.text expected String but value was a android.text.SpannableString. The default value <null> was returned.
08-19 20:19:37.379: W/Bundle(6646): Attempt to cast generated internal exception:
08-19 20:19:37.379: W/Bundle(6646): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
08-19 20:19:37.379: W/Bundle(6646): at android.os.Bundle.getString(Bundle.java:1121)
08-19 20:19:37.379: W/Bundle(6646): at com.myprojectabc.storage.Core$mainmethod$1.run(Core.java:394)
08-19 20:19:37.379: W/Bundle(6646): at android.os.Handler.handleCallback(Handler.java:733)
08-19 20:19:37.379: W/Bundle(6646): at android.os.Handler.dispatchMessage(Handler.java:95)
08-19 20:19:37.379: W/Bundle(6646): at android.os.Looper.loop(Looper.java:136)
08-19 20:19:37.379: W/Bundle(6646): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-19 20:19:37.379: W/Bundle(6646): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 20:19:37.379: W/Bundle(6646): at java.lang.reflect.Method.invoke(Method.java:515)
08-19 20:19:37.379: W/Bundle(6646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-19 20:19:37.379: W/Bundle(6646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-19 20:19:37.379: W/Bundle(6646): at dalvik.system.NativeStart.main(Native Method)
如果我然后尝试通过将其更改为这个来解决该问题...
body = mExtras.getString(Notification.EXTRA_TEXT).toString();
...我收到NullPointerException
如果有人可以在这里帮助我,我将不胜感激
谢谢,REG1
【问题讨论】:
-
文档没有显示您要查找的数据的密钥。所以要么它不存在,要么它没有记录在案。您可以遍历键并将值输出到日志以查看键是否突出。
-
您这样做的目的是什么?
-
@LarryMcKenzie 我忘了提到我检查了几个我认为可以表示“电子邮件正文”的键,但它们都返回了 null。它似乎不存在...注意关键 EXTRA_PICTURE:这是要在 Notification.BigPictureStyle 扩展通知中显示的位图,提供给 bigPicture(android.graphics.Bitmap) -> 我想要由 bigText 提供的文本(不是图片) (),但它不存在。除了 java 反射之外,还有什么快速的解决方法吗?感谢您的快速回复:)
-
而不是猜测可能的键迭代一个额外的包,看看是否存在。如果它不存在,那么您就不可能获得该数据。
-
@LarryMcKenzie 感谢您的想法 :)
标签: android notifications android-notifications android-notification-bar