我想从StatusBarNotification
-对象中获得尽可能多的信息。目前,唯一可以访问的"可靠"信息是tickerText
-属性。我使用以下代码通过RemoteViews
获取通知的标题和文本,但很多时候,标题和/或文本都是空的:-(:
//Get the title and text
String mTitle = "";
String mText = "";
try {
RemoteViews remoteView = sbn.getNotification().contentView;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null);
remoteView.reapply(getApplicationContext(), localView);
TextView tvTitle = (TextView) localView.findViewById(android.R.id.title);
TextView tvText = (TextView) localView.findViewById(16908358);
mTitle = (String)tvTitle.getText();
mText = (String)tvText.getText();
} catch (Exception e){
Log.e(TAG, "Error getting notification title/text: " + e);
}
有其他(更可靠的)方法吗?我可以"手动编码"Gmail、SMS等"流行"通知的资源ID,但当这些应用程序更新时,这可能会随时中断。谢谢
使用Android 4.4(KitKat),API 19级,您可以使用Notification.extras attibute获取通知标题、文本,。。。。
http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html
检查资源ID实际上是Android屏幕阅读器TalkBack解析通知类型的方式。它尝试直接从各种包加载ID。
查看谷歌代码上的源代码以获取完整的示例。以下是一个片段:
private static int ICON_GMAIL;
private static boolean sHasLoadedIcons = false;
private static void loadIcons(Context context) {
...
ICON_GMAIL = loadIcon(context, "com.google.android.gm",
"com.google.android.gm.R$drawable", "stat_notify_email");
sHasLoadedIcons = true;
}
public static NotificationType getNotificationTypeFromIcon(Context context, int icon) {
if (!sHasLoadedIcons) {
loadIcons(context);
}
...
if (icon == ICON_GMAIL) {
return NotificationType.EMAIL;
}
}