从状态栏的扩展通知中获取文本



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

我想使用通知来获得它。额外的,但似乎没有常数,如Notification.EXTRA_BIG_TEXT为例,允许这工作。

还有别的方法可以得到这个文本吗?此外,当我使用

String bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;

它返回null,知道为什么吗?我用它来获取电子邮件的主题(Testing the new Gmail notification shortcuts ->见下面的链接)。

下面是一个扩展Gmail通知的例子:(我想获得以下文本->

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

Gmail通知图片(声望低于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

对于这些新信息,我将尝试这样做:

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

编辑:在查看源代码后,我会尝试这样做:

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

当任何通知生成大样式通知时,可以使用android.bigText键获取扩展文本:

if(mExtras.getCharSequence("android.bigText")) {
    String body = mExtras.getCharSequence("android.bigText");
}

最新更新