PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
第一個參數連接上下文的context
第二個參數是對PendingIntent的描述,請求值不同Intent就不同
第三個參數是一個Intent對象,包含跳轉目標
第四個參數有4種狀態
FLAG_CANCEL_CURRENT:如果當前系統中已經存在一個相同的PendingIntent對象,那么就將先將已有的PendingIntent取消,然后重新生成一個PendingIntent對象。
FLAG_NO_CREATE:如果當前系統中不存在相同的PendingIntent對象,系統將不會創建該PendingIntent對象而是直接返回null。
FLAG_ONE_SHOT:該PendingIntent只作用一次。在該PendingIntent對象通過send()方法觸發過后,PendingIntent將自動調用cancel()進行銷毀,那么如果你再調用send()方法的話,系統將會返回一個SendIntentException。
FLAG_UPDATE_CURRENT:如果系統中有一個和你描述的PendingIntent對等的PendingInent,那么系統將使用該PendingIntent對象,但是會使用新的Intent來更新之前PendingIntent中的Intent對象數據,例如更新Intent中的Extras。
如果使用PendingIntent.FLAG_UPDATE_CURRENT,那么每次notifiId都是相同的數字,說明PendingIntent是一個,舊的參數被更新了。
如果使用PendingIntent.FLAG_ONE_SHOT,那么PendingIntent只是第一次有效,后來再點擊別的Notification就無效了。
是否和getActivity的第2個參數有關系呢?
確實如此,第2個參數如果不同,那么表示PendingIntent不同,就不會出現上述兩種情況。
那么問題來了,FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT有多大的區別?