【发布时间】:2026-01-09 19:00:02
【问题描述】:
我正在尝试使用 Android 4.3 的 Notification Listener API 读取通知。 我遵循了多个教程 例如。 http://gmariotti.blogspot.hk/2013/11/notificationlistenerservice-and-kitkat.html?view=magazine
这个问题也不能解决我的问题,因为我不想阅读 Big View 通知。我可以的
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
我想要做的是将这些通知中的每一个作为对象字符串或其他。我只需要这些通知中的文本、标题和源应用程序。
如您所见,当我尝试从通知中获取文本时,我当前接收的对象仅返回 Null
【问题讨论】:
-
为什么不能只使用 Notification 对象?
-
@NathanWalters 这不是我正在做的吗? sbn.getNotification().extras.getCharSequence("android.title")
-
是的。这就是为什么我对你的要求感到困惑。 “我可以使用的东西”是什么意思?您已经将 Notification 作为对象;使用它。
-
@NathanWalters 对不起,我编辑了文本以阐明我需要什么。 - 我需要的只是每个通知中的文本、标题和源应用程序。由于我已经拥有的对象只为文本返回 null
-
@user3549377,你在这个问题上有没有进一步的进展?
标签: android notifications listener