[英]Android onNewIntent always receives same Intent
I have 2 Notifications
: one for incoming messages, one for outgoing messages. 我有2个
Notifications
:一个用于传入消息,一个用于传出消息。 On Notification
click, it sends the PendingIntent
to self. 单击
Notification
,它将PendingIntent
发送给self。 I put in an extra value to determine which of the Notifications
was clicked: 我输入了一个额外的值来确定单击了哪个
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);
}
And capture the Intent
in the onNewIntent
method: 并在
onNewIntent
方法中捕获Intent
:
@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");
}
plus the manifest
line that makes sure that there's only one task (in activity
tag): 加上
manifest
行,以确保只有一项任务(在activity
标签中):
android:launchMode="singleTop"
I logged that it runs through the onNewIntent
method, but always use the same intent
(IE if I click either the IN or OUT notification
, the intent extra always contains the same bundle
(logs: OUT: false
)). 我记录到它通过
onNewIntent
方法运行,但始终使用相同的intent
(即,如果我单击IN或OUT notification
,则另外的意图总是包含相同的bundle
(日志: OUT: false
))。 It's always the Intent that was created last, which I found out because the initialization of both intents happens in another sequence than when they are changed: 我发现,这始终是最后创建的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);
}
Why do I always receive the same (last created) Intent
? 为什么我总是收到相同的(最后创建的)
Intent
?
You are passing same requestcode
for all intent that why you reciev last intent everytime, so you have to pass different requestcode
in pending intent.. 您正在为所有意图传递相同的
requestcode
,这就是为什么每次都要接收上一个意图的原因,因此您必须在挂起的意图中传递不同的requestcode
。
like as below code 像下面的代码
Your code: 您的代码:
PendingIntent pi = PendingIntent.getActivity(Entry.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Need to change: 需要改变:
PendingIntent pi = PendingIntent.getActivity(Entry.this, your_request_code, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.