我正在寻找一种方法,当有多条线路时从 WhatsApp 通知中获取消息。
我试图通过 Android 中的反射从内部类内的私有变量获取值。我正在尝试获取用于构建 InboxStyle 通知的字符序列的“mTexts”ArrayList。自上次更新以来,我正在寻找来自 Whatsapp 的消息,notificationListener() 监听的通知没有额外的多行通知(我只能获取第一行)。
任何获得线路的方式都是值得的。
这是我的代码。
@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(**WHICH OBJECT MAY USE HERE**);
}catch(IllegalAccessException e){
e.printStackTrace();
}
for(CharSequence value : mTextsArrayList){
Log.i("XXX","Results are: "+value.toString());
}
}
}
}
}
}
我正确到达了 mText 字段,但我无法从中获取值。
我尝试使用新的 Notification.InboxStyle() 对象
Notification.InboxStyle iStyleObjectToGetTheValue = new Notification.InboxStyle();
看看它是否运作良好,确实如此
inboxStyle = (ArrayList<CharSequence>) fmText.get(iStyleObjectToGetTheValue);
但我需要通知中的值。关于如何实现这一目标有什么想法吗?
我还尝试让消息行膨胀RemoteViews,您可以通过StatusBarNotification.getNotification().THE_REMOTE_VIEW检索,因为使用DeviceMonitor您可以拍摄设备的屏幕截图并查看视图的ID。 .但没有那么幸运。
任何获得线路的方式都是值得的。
欢迎所有帮助! 谢谢!
根据 RemoteView.apply() 的文档:
View apply(Context context, ViewGroupparent) - 膨胀此对象表示的视图层次结构并应用所有操作。
您可以创建父级并向
RemoteView.apply()
提供上下文。检查生成的视图以查找文本:
@Nullable
public View getBigContentView (StatusBarNotification statusBarNotification) {
RemoteViews bigContentView = statusBarNotification.getNotification().bigContentView;
return bigContentView != null ? bigContentView.apply(ctx, null) : null;
}
这可能不适用于 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)}.