检索StatusbarNotification详细信息的可靠方法(标题,通知文本)

Nic*_*ick 13 notifications android remoteview

我希望从对象中获取尽可能多的信息StatusBarNotification.现在,唯一可以访问的"可靠"信息是tickerText-property.我正在使用以下代码来获取通知的标题和文本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);
    }
Run Code Online (Sandbox Code Playgroud)

有没有替代方案(更可靠)?我可以"手动编码""热门"通知(如Gmail,短信等)的资源ID,但这可能会在这些应用程序更新时随时中断.谢谢!

ala*_*anv 3

检查资源 ID 实际上是 Android 屏幕阅读器 TalkBack 解析通知类型的方式。它尝试直接从各种包加载 ID。

查看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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Niek,完成后我将在这里分享我的解决方案。我也得到了布局膨胀异常:-/。我见过另一种解决方案,他们迭代布局 TextView id(不事先膨胀实际布局)。我看看能不能再找到它。 (2认同)