简体   繁体   English

Java反射通过Android Notification上的内部类从WhatsApp获取消息

[英]Java Reflection getting message from WhatsApp by inner class on Android Notification

I´m looking for a way to get the messages from WhatsApp notifications when there is more than one line. 我正在寻找一种方法,当有多行时,从WhatsApp通知中获取消息。

I´m trying to get the value from a private variable inside an inner class via Reflection in Android. 我试图通过Android中的Reflection从内部类中的私有变量中获取值。 I´m trying to get the 'mTexts' ArrayList of charsequence used to build an InboxStyle Notification. 我试图获取用于构建InboxStyle通知的'mTexts'charsequence的ArrayList。 I´m looking for the messages from whatsapp since the last whats update there is no EXTRA on Notifications listened by notificationListener() that have the multiple lines notification (I can only get the first line). 我正在寻找来自whatsapp的消息,因为最后一次更新没有EXTRA在notificationListener()监听的通知上有多行通知(我只能得到第一行)。

Any way to get the lines is worth it. 任何获得线路的方式都是值得的。

This is my code. 这是我的代码。

@Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);

     Class[] declaredClasses = sbn.getNotification().getClass().getClasses();

        for (Class c : declaredClasses){

            if(c.getName().contains("Notification$InboxStyle")){
                Class inboxStyleClass = c.getClass();

                Field[] fields = inboxStyleClass.getDeclaredFields();

                for(Field field : fields){

                    if(field.getName().contains("mText")){

                        Field fmText = null;
                        try {
                            fmText = inboxStyleClass.getDeclaredField("mTexts");
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        }

                        ArrayList<CharSequence> mTextsArrayList = null;

                        fmText.setAccessible(true);

                        try{
                            mTextsArrayList = (ArrayList<CharSequence>) fmText.get(**WICH OBJECT MAY USE HERE**);
                        }catch(IllegalAccessException e){
                            e.printStackTrace();
                        }

                        for(CharSequence value : mTextsArrayList){
                            Log.i("XXX","Results are: "+value.toString());
                        }
                    }
                }

            }

        }

}

I reach the mText field correctly but I can´t get the value from it. 我正确到达mText字段但我无法从中获取值。

I tried to use a new Notification.InboxStyle() object 我试图使用一个新的Notification.InboxStyle()对象

 Notification.InboxStyle iStyleObjectToGetTheValue = new Notification.InboxStyle();

to see if it works well, and it does 看它是否运作良好,确实如此

inboxStyle = (ArrayList<CharSequence>) fmText.get(iStyleObjectToGetTheValue);

but I need the values from the notification. 但我需要通知中的值。 Any idea on how can I achieve that? 关于如何实现这一点的任何想法?

I also tried to get the message lines inflating the RemoteViews that you can retrieve by StatusBarNotification.getNotification().THE_REMOTE_VIEW because using DeviceMonitor you can take an screenshoot of the device and see the IDs of the views... but had no lucky with that. 我还尝试通过StatusBarNotification.getNotification()来获取可以检索RemoteView的消息行.THE_REMOTE_VIEW因为使用DeviceMonitor你可以对设备进行屏幕截图并查看视图的ID ......但是没有幸运的话。

Any way to get the lines is worth it. 任何获得线路的方式都是值得的。

All the help is welcomed!! 欢迎所有的帮助!! Thanks!! 谢谢!!

According to the docs for RemoteView.apply(): 根据RemoteView.apply()的文档:

View apply(Context context, ViewGroup parent) - Inflates the view hierarchy represented by this object and applies all of the actions. View apply(上下文上下文,ViewGroup父级) - 对此对象表示的视图层次结构进行膨胀并应用所有操作。

You can create a parent and provide the context to RemoteView.apply() . 您可以创建父级并为RemoteView.apply()提供上下文。 Inspect the resulting View to find the text: 检查生成的视图以查找文本:

@Nullable
public View getBigContentView (StatusBarNotification statusBarNotification) {
    RemoteViews bigContentView = statusBarNotification.getNotification().bigContentView;
    return bigContentView != null ? bigContentView.apply(ctx, null) : null;
}

This might not work in Nougat phones, as according to the documentation (reading this more carefully tells me that Google probably did not mean for you to read this parameter and that it should only be used when building the notification): 根据文档的说法,这可能不适用于Nougat手机(更仔细地阅读这些内容告诉我,Google可能并不意味着您阅读此参数并且只应在构建通知时使用它):

  * As of N, this field may be null. The expanded notification view is determined by the * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM