我希望从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,短信等)的资源ID,但这可能会在这些应用程序更新时随时中断。谢谢!
答案 0 :(得分:4)
使用Android 4.4(KitKat),API等级19,您可以使用Notification.extras属性来获取通知标题,文本,....
http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html
答案 1 :(得分:3)
检查资源ID实际上是Android屏幕阅读器TalkBack如何解析通知类型。它试图直接从各种包中加载ID。
查看source on Google Code以获取完整示例。这是一个片段:
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;
}
}