[英]Java Reflection getting message from WhatsApp by inner class on Android Notification
我正在尋找一種方法,當有多行時,從WhatsApp通知中獲取消息。
我試圖通過Android中的Reflection從內部類中的私有變量中獲取值。 我試圖獲取用於構建InboxStyle通知的'mTexts'charsequence的ArrayList。 我正在尋找來自whatsapp的消息,因為最后一次更新沒有EXTRA在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(**WICH 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);
但我需要通知中的值。 關於如何實現這一點的任何想法?
我還嘗試通過StatusBarNotification.getNotification()來獲取可以檢索RemoteView的消息行.THE_REMOTE_VIEW因為使用DeviceMonitor你可以對設備進行屏幕截圖並查看視圖的ID ......但是沒有幸運的話。
任何獲得線路的方式都是值得的。
歡迎所有的幫助!! 謝謝!!
根據RemoteView.apply()的文檔:
View apply(上下文上下文,ViewGroup父級) - 對此對象表示的視圖層次結構進行膨脹並應用所有操作。
您可以創建父級並為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)}.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.