从状态栏中的扩展通知中获取文字? [英] Get text from expanded notification in status bar?

查看:148
本文介绍了从状态栏中的扩展通知中获取文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在statusbar的扩展通知中获取文本?这意味着文本设置了我的Notification.BitTextStyle.bigText(),例如来自Gmail的电子邮件通知.

Is it possible to get the text within an expanded notification in the statusbar? This means text set my Notification.BitTextStyle.bigText(), such as in an email notification from Gmail.

我想使用Notification.extras来获取它,但是似乎没有常量(例如Notification.EXTRA_BIG_TEXT)可以使它工作.

I would like to get it using Notification.extras but there seems to be no constant, such as Notification.EXTRA_BIG_TEXT for example, that allows this to work.

还有另一种获取此文本的方法吗? 此外,当我使用

Is there another way of getting this text? Furthermore when I use

字符串bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;

String bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;

返回null,为什么?我用它来获取电子邮件的主题(Testing the new Gmail notification shortcuts->请参阅下面的链接).

it returns null, any idea why? I use this to get the subject of the email (Testing the new Gmail notification shortcuts -> see link below).

下面是展开的Gmail通知的示例:(我想获得以下文本->

Below is an example of an expanded Gmail notification: (I want to get the following text ->

您现在可以在...中获得回复"和存档"的快捷按钮.

You now get shortcut buttons for Reply and Archive within...

Gmail通知图片 (抱歉,无法发布信誉低于10的图片)

Picture of Gmail Notification (Can't post pictures with less than 10 reputation, sorry)

我还尝试了以下方法:

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());

但这对我也不起作用,似乎从未从任何视图中获取任何文本.

But that didn't work for me either, never seemed to get any Text from any of the views.

更新:

当我使用以下代码从通知中获取文字...

When I use the following code to get the text from a notification...:

body = mExtras.getString(Notification.EXTRA_TEXT);

...只要不是Gmail/电子邮件通知,一切都很好:

...all works well as long as it is not a Gmail/Email notification:

我收到Gmail通知后,会收到以下错误消息:

As soon as I get a Gmail notification I get the following error message:

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)

如果我接下来尝试通过将其更改为此问题来解决该问题...

If I then try to solve that problem by changing it to this...

body = mExtras.getString(Notification.EXTRA_TEXT).toString();

...我得到一个NullPointerException

如果有人可以在这里帮助我,我将非常感谢

Would really appreciate it if someone could help me out here

感谢REG1

推荐答案

好吧,有了这些新信息,我会尝试这样的事情:

Okay so with this new information I would try something like this:

SpannableString bigText = (SpannableString) mExtras.get(Notification.EXTRA_TEXT);
if(bigText != null){
    body = bigText.toString();
}

在查看了源代码之后,我会尝试以下方法:

After reviewing the source I would try this:

CharSequence bigText = (CharSequence) mExtras.getCharSequence(Notification.EXTRA_TEXT);
if(bigText != null){
    body = bigText.toString();
}

这篇关于从状态栏中的扩展通知中获取文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆