Retrieve text from a RemoteViews Object

前端 未结 4 2208
北荒
北荒 2021-02-09 22:25

I need to retrieve some text from a RemoteViews object. It is possible for me to get the LayoutId, but I have no idea how to retrieve text from a TextView

4条回答
  •  心在旅途
    2021-02-09 23:13

    Taken from Extract notification text from parcelable, contentView or contentIntent :

    Notification notification = (Notification) event.getParcelableData();
    RemoteViews views = notification.contentView;
    Class secretClass = views.getClass();
    
    try {
        Map text = new HashMap();
    
        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {
            if (!outerFields[i].getName().equals("mActions")) continue;
    
            outerFields[i].setAccessible(true);
    
            ArrayList actions = (ArrayList) 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();
    }
    
        

    提交回复
    热议问题