2017-04-07 44 views
1

我正在尋找一種方法來從WhatsApp通知中獲取消息,當有多行時。Java反射從Android上的內部類獲取WhatsApp的消息通知

我想通過Android中的Reflection從內部類中的私有變量中獲取值。我正嘗試獲取用於構建InboxStyle通知的charsequence的'mTexts'ArrayList。我正在尋找whatsapp自最後一次更新之後的消息,其中沒有EXTRA on notification by listeListener(),它具有多行通知(我只能得到第一行)。

任何獲得線條的方法都是值得的。

這是我的代碼。

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

      } 

     } 

} 

我正確地到達多行文字場,但我不能得到它的價值。

我試圖用一個新的Notification.InboxStyle()對象

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

,看它是否運作良好,它也

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

,但我需要從通知的值。任何想法,我怎麼能實現這一點?

我也試圖得到消息系充氣RemoteViews,您可以通過StatusBarNotification.getNotification()檢索。THE_REMOTE_VIEW因爲使用設備監視器您可以對設備的screenshoot看到的的ID意見......但沒有幸運的。

任何獲得線條的方法都是值得的。

所有的幫助是歡迎的! 謝謝!

+0

你能不能從通知獲取內容查看/ bigContentView並檢查他們找到文本:建立通知)時,才應使用? –

+0

如何檢查遠程視圖? - 我試圖通過獲取視圖節點進行搜索,但沒有發現任何可靠的...也許我做錯了... – Dan

回答

0

按照文檔的RemoteView.apply():

查看應用(上下文的背景下,一個ViewGroup父) - 膨脹該對象表示視圖層級和所有適用的行動。

您可以創建一個父級並向RemoteView.apply()提供上下文。檢查結果視圖查找的文字:

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

這可能不是在牛軋糖手機上運行,​​如根據文件(閱讀更細緻地告訴我,谷歌可能並不意味着你閱讀此參數和

* 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)}. 
+0

當試圖膨脹bigContentView()時,我得到空指針。這是我的代碼... RemoteViews rv = sbn.getNotification()。bigContentView; LayoutInflater inflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup localView =(ViewGroup)inflater.inflate(rv.getLayoutId(),null); rv.reapply(ctx.getApplicationContext(),localView);對於(int i = 0; i <((ViewGroup)localView).getChildCount(); ++ i){ 查看nextChild =((ViewGroup)localView).getChildAt(i); Log.i(「XXX」,「ID:」+ nextChild.getId()); } – Dan

+0

沒有幸運的是...沒有任何東西可以使用...只有2個ID沒有興趣。我怎麼知道這個ID是否屬於這個ID? – Dan