检索StatusbarNotification细节的可靠的方式(标题,通知文本)

问题描述:

我想获得尽可能多的信息了 StatusBarNotification -object的越好。现在,可访问的唯一可靠的信息是 tickerText -property。我用下面的code获得通过 RemoteViews 的通知的标题和正文,但很多时候,标题和/或文本就干脆为空: - (:

I'd like to get as much information out of a StatusBarNotification-object as possible. Right now, the only "reliable" information that can be accessed is the tickerText-property. I'm using the following code to get the notification's title and text via RemoteViews, but a lot of the time, the title and/or text will simply be null :-(:

    //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);
    }

有没有替代的(更可靠)的方式?我可以手工code的资源,如Gmail,短信等冷门的通知的ID,但是这可能随时断裂时,这些应用程序将被更新。谢谢!

Is there any alternative (more reliable) way? I could "hand-code" the resource IDs for "popular" notifications like Gmail, SMS, etc., but this may break at any time when those apps are updated. Thanks!

使用的Andr​​oid 4.4(奇巧),API等级19,你可以用Notification.extras attibute得到通知标题,文字....

With Android 4.4(KitKat), API Level 19 you can use Notification.extras attibute to get Notification title,text,....

http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html