我有 2 个Notifications
:一个用于传入消息,一个用于传出消息。Notification
单击时,它将发送PendingIntent
给自己。我输入了一个额外的值来确定哪个Notifications
被点击:
private static final int INID = 2;
private static final int OUTID = 1;
private void update(boolean incoming, String title, String message, int number) {
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, Entry.class);
intent.putExtra((incoming ? "IN" : "OUT"), incoming);
PendingIntent pi = PendingIntent.getActivity(Entry.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification noti = new Notification(incoming ? R.drawable.next : R.drawable.prev, incoming ? "Incoming message" : "Outgoing message", System.currentTimeMillis());
noti.flags |= Notification.FLAG_NO_CLEAR;
noti.setLatestEventInfo(this, title, message, pi);
noti.number = number;
notificationManager.notify(incoming ? INID : OUTID, noti);
}
Intent
并在方法中捕获onNewIntent
:
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
if (intent.getExtras() != null)
for (String id : new ArrayList<String>(intent.getExtras().keySet())) {
Object v = intent.getExtras().get(id);
System.out.println(id + ": " + v);
}
else
log("onNewIntent has no EXTRAS");
}
加上manifest
确保只有一项任务(在activity
标签中)的行:
android:launchMode="singleTop"
我记录了它通过该onNewIntent
方法运行,但始终使用相同的intent
(即,如果我单击 IN 或 OUT notification
,则额外的意图始终包含相同的bundle
(日志:)OUT: false
)。它总是最后创建的 Intent,我发现这是因为两个 Intent 的初始化发生在另一个序列中,而不是在它们被更改时:
private void buttonClick(View v) {
update(true, "IN", "in", 1);
update(false, "OUT", "out", 3);
}
private void setNotificationSettings() {
update(false, "IN", "===out message===", 0);
update(true, "OUT", "===in message===", 0);
}
为什么我总是收到相同的(最后创建的)Intent
?