从parcelable,内容查看或contentIntent提取通知文本 [英] Extract notification text from parcelable, contentView or contentIntent

查看:141
本文介绍了从parcelable,内容查看或contentIntent提取通知文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我得到了我的AccessibilityService具有以下code工作:

So I got my AccessibilityService working with the following code:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        List<CharSequence> notificationList = event.getText();
        for (int i = 0; i < notificationList.size(); i++) {
            Toast.makeText(this.getApplicationContext(), notificationList.get(i), 1).show();
        }
    }
}

它工作正常的读出文本创建notifcation时所显示的(1)

唯一的问题是,我还需要的值(3)当用户打开通知栏时显示。 (2)是不是对我很重要,但它会很高兴知道怎么读出来。正如你可能知道,所有的值可以是不同的。

The only problem is, I also need the value of (3) which is displayed when the user opens the notification bar. (2) is not important for me, but it would be nice to know how to read it out. As you probably know, all values can be different.

所以,我怎么能读出的(3)?我怀疑这是不可能的,但我的 notificationList 似乎只有一个入口(至少只有一个土司显示)。

So, how can I read out (3)? I doubt this is impossible, but my notificationList seems to have only one entry (at least only one toast is shown).

非常感谢!

/编辑:我可以用

if (!(parcel instanceof Notification)) {
            return;
        }
        final Notification notification = (Notification) parcel;

不过,我不知道如何提取notifcation的消息无论是从的通知 notification.contentView / notification.contentIntent

任何想法?

/编辑:澄清在这里问:我如何能够读出(3)

推荐答案

我已经浪费了几个小时的最后几天找出一种方法,做你(和,我也一样,顺便)的想做的事情。后通过RemoteViews的全部源找了两次,我想完成这一任务的唯一办法就是好老,丑和哈克Java的思考。

I've wasted a few hours of the last days figuring out a way to do what you (and, me too, by the way) want to do. After looking through the whole source of RemoteViews twice, I figured the only way to accomplish this task is good old, ugly and hacky Java Reflections.

这是:

    Notification notification = (Notification) event.getParcelableData();
    RemoteViews views = notification.contentView;
    Class secretClass = views.getClass();

    try {
        Map<Integer, String> text = new HashMap<Integer, String>();

        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {
            if (!outerFields[i].getName().equals("mActions")) continue;

            outerFields[i].setAccessible(true);

            ArrayList<Object> actions = (ArrayList<Object>) outerFields[i]
                    .get(views);
            for (Object action : actions) {
                Field innerFields[] = action.getClass().getDeclaredFields();

                Object value = null;
                Integer type = null;
                Integer viewId = null;
                for (Field field : innerFields) {
                    field.setAccessible(true);
                    if (field.getName().equals("value")) {
                        value = field.get(action);
                    } else if (field.getName().equals("type")) {
                        type = field.getInt(action);
                    } else if (field.getName().equals("viewId")) {
                        viewId = field.getInt(action);
                    }
                }

                if (type == 9 || type == 10) {
                    text.put(viewId, value.toString());
                }
            }

            System.out.println("title is: " + text.get(16908310));
            System.out.println("info is: " + text.get(16909082));
            System.out.println("text is: " + text.get(16908358));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这code正常工作的一个的Nexus S与Android 4.0.3。但是,我没有,如果它与其他版本的Andr​​oid测试。这很可能是一些值,尤其是的ViewID改变。我觉得应该有办法,以支持所有Android版本没有硬编码的所有可能的ID,但是这是回答另一个问题...;)

This code worked fine on a Nexus S with Android 4.0.3. However, I didn't test if it works on other versions of Android. It's very likely that some values, especially the viewId changed. I think there should be ways to support all versions of Android without hard-coding all possible ids, but that's the answer to another question... ;)

PS:你要找的(指为(3)在你原来的问题)是文字值的值

PS: The value you're looking for (referring to as "(3)" in your original question) is the "text"-value.

这篇关于从parcelable,内容查看或contentIntent提取通知文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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