Android - 获取通知堆叠时的通知(通知监听器)

4

我正在尝试使用Android 4.3的Notification Listener API读取通知。

我已经按照多个教程进行了操作,例如 http://gmariotti.blogspot.hk/2013/11/notificationlistenerservice-and-kitkat.html?view=magazine

此外,这个问题并不能解决我的问题,因为我不是要读取Big View通知。我可以做到那一点。

如何从Android通知中获取详细信息/展开文本?

这是我的代码:

    Log.d(logtag,"CharSequence - Title: "+sbn.getNotification().extras.getCharSequence("android.title")+
            " CharSequence - textLines: "+sbn.getNotification().extras.getCharSequence("android.textLines")+
            " CharSequence - subText: "+sbn.getNotification().extras.getCharSequence("android.subText")+
            " CharSequence - text: "+sbn.getNotification().extras.getCharSequence("android.text")+
            " CharSequence - infoText: "+sbn.getNotification().extras.getCharSequence("android.infoText")+
            " CharSequence - summaryText: "+sbn.getNotification().extras.getCharSequence("android.summaryText"));

    Log.d(logtag,"String - Title: "+sbn.getNotification().extras.getString("android.title")+
            " String - textLines: "+sbn.getNotification().extras.getString("android.textLines")+
            " String - subText: "+sbn.getNotification().extras.getString("android.subText")+
            " String - text: "+sbn.getNotification().extras.getString("android.text")+
            " String - infoText: "+sbn.getNotification().extras.getString("android.infoText")+
            " String - summaryText: "+sbn.getNotification().extras.getString("android.summaryText"));


    Log.d(logtag, "--------------------------------------------------------------------------------------------");

    Notification mNotification=sbn.getNotification();
    if (mNotification!=null){
        Bundle extras = mNotification.extras;

        String notificationTitle =
                extras.getString(Notification.EXTRA_TITLE);
        Bitmap notificationLargeIcon =
                ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
        CharSequence notificationText =
                extras.getCharSequence(Notification.EXTRA_TEXT);
        CharSequence notificationSubText =
                extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
        CharSequence notificationTextLines =
                extras.getCharSequence("android.textLines");
        Log.d(logtag, "New Title: "+notificationTitle);
        Log.d(logtag, "New Text: "+notificationText);
        Log.d(logtag, "New subText: "+notificationSubText);
        Log.d(logtag, "New textLines: "+notificationTextLines);

        Log.d(logtag, "--------------------------------------------------------------------------------------------");

这是我从截图中获取的ADB输出:

D/AANN - NotificationListener﹕ CharSequence - Title: Alex CharSequence -    textLines: null CharSequence - subText: null CharSequence - text: null CharSequence - infoText: null CharSequence - summaryText: 8 new messages.
D/AANN - NotificationListener﹕ String - Title: Alex String - textLines: null String - subText: null String - text: null String - infoText: null String - summaryText: 8 new messages.

D/AANN - NotificationListener﹕ New Title: WhatsApp
D/AANN - NotificationListener﹕ New Text: null
D/AANN - NotificationListener﹕ New subText: null
D/AANN - NotificationListener﹕ New textLines: null

我想做的是获取每个通知的对象字符串或其他内容。我只需要每个通知的文本、标题和源应用程序。
如您所见,我当前收到的对象只在我尝试从通知中获取文本时返回 Null。

http://i.stack.imgur.com/t09Ns.png


为什么你不能直接使用通知对象工作呢? - Nathan Walters
@NathanWalters 这不是我已经在做的吗? sbn.getNotification().extras.getCharSequence("android.title") - user3549377
是的。这就是为什么我对你的问题感到困惑。你所说的“可以使用的东西”是什么意思?你已经有了通知作为一个对象;就用它。 - Nathan Walters
@user3549377,你在这个问题上有进展了吗? - AndroidDev
@user3549377,你找到解决这个问题的方法了吗?我也遇到了同样的情况。请更新! - 21stking
显示剩余2条评论
2个回答

0
在我的环境中,下面的代码对我有效。

我的解决方法

sbn.getNotification().extras.getCharSequence("android.title")

sbn.getNotification().extras.get("android.title")

sbn.getNotification().extras.get(“android.title”)


0

由于通知现在使用自定义视图,所以我们需要使用一些技巧。

您也可以搜索“remote view gettext”...

从RemoteViews对象检索文本

public void onNotificationPosted(StatusBarNotification sbn) {
    Notification mNotification = sbn.getNotification();
    Log.v("mNotification.toString()", getText(mNotification).toString());

//

public static List<String> getText(Notification notification)
{
    // We have to extract the information from the view
    RemoteViews        views = notification.bigContentView;
    if (views == null) views = notification.contentView;
    if (views == null) return null;

    // Use reflection to examine the m_actions member of the given RemoteViews object.
    // It's not pretty, but it works.
    List<String> text = new ArrayList<String>();
    try
    {
        Field field = views.getClass().getDeclaredField("mActions");
        field.setAccessible(true);

        @SuppressWarnings("unchecked")
        ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field.get(views);

        // Find the setText() and setTime() reflection actions
        for (Parcelable p : actions)
        {
            Parcel parcel = Parcel.obtain();
            p.writeToParcel(parcel, 0);
            parcel.setDataPosition(0);

            // The tag tells which type of action it is (2 is ReflectionAction, from the source)
            int tag = parcel.readInt();
            if (tag != 2) continue;

            // View ID
            parcel.readInt();

            String methodName = parcel.readString();
            if (methodName == null) continue;

            // Save strings
            else if (methodName.equals("setText"))
            {
                // Parameter type (10 = Character Sequence)
                parcel.readInt();

                // Store the actual string
                String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim();
                text.add(t);
            }

            // Save times. Comment this section out if the notification time isn't important
            else if (methodName.equals("setTime"))
            {
                // Parameter type (5 = Long)
                parcel.readInt();

                String t = new SimpleDateFormat("h:mm a").format(new Date(parcel.readLong()));
                text.add(t);
            }

            parcel.recycle();
        }
    }

    // It's not usually good style to do this, but then again, neither is the use of reflection...
    catch (Exception e)
    {
        Log.e("NotificationClassifier", e.toString());
    }

    return text;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接