1. 程式人生 > >Android 點選通知欄,傳遞intent資料到已存在的activity

Android 點選通知欄,傳遞intent資料到已存在的activity

其實不是原創,是問郭神問來的.在此感謝郭霖. 以防自己忘記,也造福大家,每個人都去問郭神的話他也忙不過來,所以就寫上來了.

activity已經存在,從通知欄點進去不希望這個activity重新建立,只是希望拿到intent帶過來的資料的話, 重要的是新增的那一系列flag.

Intent intent = new Intent(NOTIFIER_NAME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Random random = new Random(System.currentTimeMillis());
int randomId = random.nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, randomId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent);
notificationManager.notify(randomId, builder.build());