FLAG_ONE_SHOT으로 PendingIntent를 생성하면 FLAG_NO_CREATE를 가진 후속 PendingIntent가 null을 반환합니다. I가 처음 출원 의도를 변경하는 경우AlarmManager에 FLAG_ONE_SHOT으로 설정된 알람이 있는지 확인하는 방법
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmService.class);
PendingIntent pi = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_ON_SHOT);
GregorianCalendar alarmtime = new GregorianCalendar(now.get(GregorianCalendar.YEAR),now.get(GregorianCalendar.MONTH),now.get(GregorianCalendar.DAY_OF_MONTH),0,0);
//Set the alarm
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
am.set(AlarmManager.RTC_WAKEUP,alarmtime.getTimeInMillis(), pi);
} else {
am.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
}
//Now check if the alarm was set, if it was set, the following PendingIntent should return not null but it doesn't
PendingIntent piCheck = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_NO_CREATE);
if (piCheck!=null) {
Log.d(TAG,"piCheck returned NOT NULL and probably returned pi");
} else if (piCheck==null) {
Log.d(TAG,"piCheck returned NULL pi does not exist");
그러나 :
PendingIntent pi = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_CANCEL_CURRENT);
예상대로 다음 나의 두 번째 PendingIntent는 null를 돌려줍니다.
PendingIntents가 알람을 제대로 설정했지만 FLAG_ONE_SHOT PendingIntent를 "확인할"수 없습니다. 이 행동의 이유는 무엇입니까? 그 목적은 무엇입니까?
나는 혼란스럽게 생각합니다. 'PendingIntent.FLAG_NO_CREATE' 플래그를 가진'getService()'를 호출하면'null'을 반환해야한다고 생각하게하는 이유는 무엇입니까? 'PendingIntent'가 존재하지 않으면'null' 만 리턴합니다. 그러나, 이전에'PendingIntent'를 만들었으므로 여전히 존재합니다. 나는'PendingIntent.FLAG_CANCEL_CURRENT' 플래그를 사용하면'PendingIntent'를 새로 생성하기 때문에 그것이 왜 널을 반환하는지 알지 못합니다. 설명 해주십시오. –
죄송합니다. null과 not null이 섞여 있습니다. FLAG_NO_CREATE를 사용하면 PendingIntent가 있더라도 null을 반환합니다. FLAG_CANCEL_CURRENT는 예상대로 작동합니다. – aajiwa
나는 아직도 당신이 혼란 스럽다고 생각합니다. 'PendingIntent'를'FLAG_ONE_SHOT'으로 생성하면이'PendingIntent는 한 번만 사용할 수 있습니다. 일단 사용되면 삭제됩니다. FLAG_CANCEL_CURRENT를 지정하면 항상 ** 새로운 ** PendingIntent를 생성하므로 ** null을 반환하지 않습니다. –