Get text from expanded notification in status bar?

二次信任 提交于 2019-12-07 07:19:16

问题


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.

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

String bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;

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).

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...

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

As an alternative I have also tried the following:

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.

Update:

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

body = mExtras.getString(Notification.EXTRA_TEXT);

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

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

...I get a NullPointerException

Would really appreciate it if someone could help me out here

Thanks, REG1


回答1:


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

Edit: After reviewing the source I would try this:

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



回答2:


When any notification generates Big Style Notification, expanded text can be fetched with android.bigText key :

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


来源:https://stackoverflow.com/questions/25292903/get-text-from-expanded-notification-in-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!