0

NotificationListenerService用来从通知中检索信息。实际上,我只能在文本视图中显示我在通知栏中看到的最后一个通知。如果可能,我希望过滤此方法并仅显示 whatsapp 通知。我可以获取包名称并将其显示在 LOG 中,但我不知道我是否进行了过滤。这是 NotificaionListenerService 的一部分

public void onNotificationPosted(StatusBarNotification sbn) {
        Notification mNotification=sbn.getNotification();
        if (mNotification!=null){
            Bundle extras = mNotification.extras;

            Intent intent = new Intent(MainActivity.INTENT_ACTION_NOTIFICATION);
            intent.putExtras(mNotification.extras);
            sendBroadcast(intent);

            TAG = "onNotificationPosted";
            Log.i(TAG,"Package Name" + sbn.getPackageName());

            Notification.Action[] mActions=mNotification.actions;
            if (mActions!=null){
                for (Notification.Action mAction:mActions){
                    int icon=mAction.icon;
                    CharSequence actionTitle=mAction.title;
                    PendingIntent pendingIntent=mAction.actionIntent;
                }
            }
        }
    }

我可以在其中看到创建通知的应用程序的包名称。我必须在那里制作过滤器吗?我该怎么做?谢谢

4

1 回答 1

1

就像这样做一样简单:

if("com.the.package.name.you.want.to.filter".equals(sbn.getPackageName())) {
     //Do something
}

或者如果有多个,您可以将它们放在哈希图或列表中,并查看包是否在其中。

于 2014-04-21T19:28:01.160 回答