2014-06-14 165 views
4

我想閱讀使用Android 4.3的Notification Listener API的通知。 我已經關注了多個教程 E.g. http://gmariotti.blogspot.hk/2013/11/notificationlistenerservice-and-kitkat.html?view=magazineAndroid - 獲取通知時,他們堆疊(通知偵聽器)

此外這個問題不能解決我的問題,因爲我不想閱讀大視圖通知。我能做到這一點

Getting Detail/Expanded Text from an Android Notification?

這是我的代碼:

Log.d(logtag,"CharSequence - Title: "+sbn.getNotification().extras.getCharSequence("android.title")+ 
      " CharSequence - textLines: "+sbn.getNotification().extras.getCharSequence("android.textLines")+ 
      " CharSequence - subText: "+sbn.getNotification().extras.getCharSequence("android.subText")+ 
      " CharSequence - text: "+sbn.getNotification().extras.getCharSequence("android.text")+ 
      " CharSequence - infoText: "+sbn.getNotification().extras.getCharSequence("android.infoText")+ 
      " CharSequence - summaryText: "+sbn.getNotification().extras.getCharSequence("android.summaryText")); 

    Log.d(logtag,"String - Title: "+sbn.getNotification().extras.getString("android.title")+ 
      " String - textLines: "+sbn.getNotification().extras.getString("android.textLines")+ 
      " String - subText: "+sbn.getNotification().extras.getString("android.subText")+ 
      " String - text: "+sbn.getNotification().extras.getString("android.text")+ 
      " String - infoText: "+sbn.getNotification().extras.getString("android.infoText")+ 
      " String - summaryText: "+sbn.getNotification().extras.getString("android.summaryText")); 


    Log.d(logtag, "--------------------------------------------------------------------------------------------"); 

    Notification mNotification=sbn.getNotification(); 
    if (mNotification!=null){ 
     Bundle extras = mNotification.extras; 

     String notificationTitle = 
       extras.getString(Notification.EXTRA_TITLE); 
     Bitmap notificationLargeIcon = 
       ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON)); 
     CharSequence notificationText = 
       extras.getCharSequence(Notification.EXTRA_TEXT); 
     CharSequence notificationSubText = 
       extras.getCharSequence(Notification.EXTRA_SUB_TEXT); 
     CharSequence notificationTextLines = 
       extras.getCharSequence("android.textLines"); 
     Log.d(logtag, "New Title: "+notificationTitle); 
     Log.d(logtag, "New Text: "+notificationText); 
     Log.d(logtag, "New subText: "+notificationSubText); 
     Log.d(logtag, "New textLines: "+notificationTextLines); 

     Log.d(logtag, "--------------------------------------------------------------------------------------------"); 

,這是我從截圖輸出ADB:

D/AANN - NotificationListener﹕ CharSequence - Title: Alex CharSequence - textLines: null CharSequence - subText: null CharSequence - text: null CharSequence - infoText: null CharSequence - summaryText: 8 new messages. 
D/AANN - NotificationListener﹕ String - Title: Alex String - textLines: null String - subText: null String - text: null String - infoText: null String - summaryText: 8 new messages. 

D/AANN - NotificationListener﹕ New Title: WhatsApp 
D/AANN - NotificationListener﹕ New Text: null 
D/AANN - NotificationListener﹕ New subText: null 
D/AANN - NotificationListener﹕ New textLines: null 

我試圖做的是獲取每個通知作爲對象字符串或其他。我需要的是來自每個通知的文本,標題和源應用程序。

正如你所看到的,我目前只接收對象返回時,我試圖從通知

http://i.stack.imgur.com/t09Ns.png

+0

爲什麼你就不能與通知對象的工作? –

+0

@NathanWalters 這不就是我在做什麼嗎? sbn.getNotification()。extras.getCharSequence(「android.title」) – user3549377

+0

就是這樣。這就是爲什麼我對你所問的問題感到困惑。 「我可以合作的東西」是什麼意思?您已將通知作爲對象;與此一起工作。 –

回答

0

獲得文本,因爲通知使用海關現在查看空,所以我們需要使用一些技巧。

您可以搜索 「遠程視圖的gettext」 還...

Retrieve text from a RemoteViews Object

public void onNotificationPosted(StatusBarNotification sbn) { 
    Notification mNotification = sbn.getNotification(); 
    Log.v("mNotification.toString()", getText(mNotification).toString()); 

//

public static List<String> getText(Notification notification) 
{ 
    // We have to extract the information from the view 
    RemoteViews  views = notification.bigContentView; 
    if (views == null) views = notification.contentView; 
    if (views == null) return null; 

    // Use reflection to examine the m_actions member of the given RemoteViews object. 
    // It's not pretty, but it works. 
    List<String> text = new ArrayList<String>(); 
    try 
    { 
     Field field = views.getClass().getDeclaredField("mActions"); 
     field.setAccessible(true); 

     @SuppressWarnings("unchecked") 
     ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field.get(views); 

     // Find the setText() and setTime() reflection actions 
     for (Parcelable p : actions) 
     { 
      Parcel parcel = Parcel.obtain(); 
      p.writeToParcel(parcel, 0); 
      parcel.setDataPosition(0); 

      // The tag tells which type of action it is (2 is ReflectionAction, from the source) 
      int tag = parcel.readInt(); 
      if (tag != 2) continue; 

      // View ID 
      parcel.readInt(); 

      String methodName = parcel.readString(); 
      if (methodName == null) continue; 

      // Save strings 
      else if (methodName.equals("setText")) 
      { 
       // Parameter type (10 = Character Sequence) 
       parcel.readInt(); 

       // Store the actual string 
       String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim(); 
       text.add(t); 
      } 

      // Save times. Comment this section out if the notification time isn't important 
      else if (methodName.equals("setTime")) 
      { 
       // Parameter type (5 = Long) 
       parcel.readInt(); 

       String t = new SimpleDateFormat("h:mm a").format(new Date(parcel.readLong())); 
       text.add(t); 
      } 

      parcel.recycle(); 
     } 
    } 

    // It's not usually good style to do this, but then again, neither is the use of reflection... 
    catch (Exception e) 
    { 
     Log.e("NotificationClassifier", e.toString()); 
    } 

    return text; 
}