Android棒棒糖阅读通知文本

时间:2015-07-01 11:45:08

标签: android notifications android-5.1.1-lollipop

我在Android 5.0上测试我的应用程序,我发现我无法从第三方通知中获取RemoteViews来读取其标题和自动收录器文本,就像我在KitKat中所做的那样。我在KitKat上成功使用的代码与此类似:

public static List<String> getText(Notification notification) {
      RemoteViews views = notification.contentView;
        if (views == null)
            return null;
        else  {
            ...
        }
    }

此函数返回NULL,因此无法从通知中获取整个contentView。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

  

覆盖onAccessibilityEvent

 @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {

            }
        }
    }
  

获取通知

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