ste*_*ong 1 android android-intent android-notifications
我有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);
}
Run Code Online (Sandbox Code Playgroud)
而捕捉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");
}
Run Code Online (Sandbox Code Playgroud)
加上manifest确保只有一个任务(在activity标签中)的行:
android:launchMode="singleTop"
Run Code Online (Sandbox Code Playgroud)
我记录它运行通过该onNewIntent方法,但总是使用相同的intent(如果我点击IN或OUT notification,IE ,额外的intent总是包含相同的bundle(logs :) OUT: false).它始终是最后创建的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);
}
Run Code Online (Sandbox Code Playgroud)
为什么我总是收到相同的(最后创建的)Intent?
你requestcode为所有意图传递相同的意思,为什么你每次都收回最后的意图,所以你必须传递不同requestcode的待决意图..
如下面的代码
你的代码:
PendingIntent pi = PendingIntent.getActivity(Entry.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)
需要改变:
PendingIntent pi = PendingIntent.getActivity(Entry.this, your_request_code, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2249 次 |
| 最近记录: |