如何将StatusBarNotification传递给Activity

时间:2015-01-20 10:01:43

标签: android

我对Notification Listener Service的经验较少。尝试通过Android 4.4上的intent传递StatusBarNotification作为参数。我不知道这是不可能的。

以下是通知侦听器服务:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {

            intent = new Intent("INTENT_ACTION_NOTIFICATION");
            intent.putExtra("sbn", sbn); //don't know possible or not
            sendBroadcast(intent);  

    }
}

这是活动接收者:

private class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {

            //How get StatusBarNotification here????????
        }

    }
}

我还尝试传递数据:

intent = new Intent("INTENT_ACTION_NOTIFICATION");
intent.putExtra("sbn",sbn.getPackageName()); 
sendBroadcast(intent);  

没问题。

但需要通过“StatusBarNotification'”。 如果无法通过' StatusBarNotification'通过意图,如果有任何其他方式,请告诉我...

...谢谢

2 个答案:

答案 0 :(得分:2)

StatusBarNotification确实实现了Parcelable接口,因此它应该倾向于Intent转移,尽管我从未亲自尝试过。 使用它在接收器中检索它:

private class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            StatusBarNotification myNotification = (StatusBarNotification)intent.getParcelableExtra("sbn");
            if(myNotification != null) {
                //StatusBarNotification successfully retrieved
            }
        }

    }
}

编辑:Here是Google文档的链接,供进一步研究使用。

答案 1 :(得分:0)

如果需要,请使用Parcelable或创建自己的parcelable类对象。

private class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        StatusBarNotification myNotification = (StatusBarNotification)intent.getParcelableExtra("sbn");
        if(myNotification != null) {
            //StatusBarNotification successfully retrieved
        }
    }

}
}