https://itstory1592.tistory.com/127
[Android] PendingIntent 공식문서 파헤치기
해당 포스팅은 Android 공식문서를 읽고 정리한 내용을 바탕으로 작성하였습니다. https://developer.android.com/reference/android/app/PendingIntent PendingIntent | Android Developersdeveloper.android.com A description of an Intent
itstory1592.tistory.com
PendingIntent
- getActivity(), getActivities(), getBroadcast(), getService() 메서드를 통해 생성할 수 있다.
- PendingIntent 에 정의된 action 은 다른 애플리케이션에서 수행될 수 있다.
- 다른 애플리케이션에 PendingIntent 를 전달하면, 마치 자신의 Intent 인 것처럼 작업을 수행할 권한을 얻는다.
- PendingIntent 는 시스템에서 유지하는 토큰에 대한 참조일 뿐이다. 자체적으로 무언가 할 수 있는 객체는 아니다.
- 중요한 것은 PendingIntent 가 감싸고 있는 Intent에 정의된 여러 데이터 집합들이다.
- PendingIntent 를 생성한 애플리케이션이 종료되어도, 시스템에서 이를 유지하기 때문에 다른 애플리케이션에서 해당 PendingIntent 를 제공받아 작업을 할 수 있다.
- Request Code, Intent.filterEquals(Intent) + operation + PendingIntent.flags 가 참인 Intent 에 대해서는 동일한 PendingIntent 를 반환받는다.
- Request Code : PendingIntent 를 구분하기 위한 고유 식별자
- Intent.filterEquals(Intent) :
operation, action, data, categories, components,Intent.flags+ type 으로 정의된 intent 를 비교 - operation : getActivity(), getService(), getBroadcast() 중 어떤 메서드를 썼는지
- Extra data 는 비교 대상이 아니다.
- PendingIntent 를 cancel() 메서드를 통해 제거할 수 있다.
- API 31+부터 FLAG_IMMUTABLE 과 FLAG_MUTABLE 중 하나를 지정해주지 않으면 런타임 에러가 발생한다.
- PendingIntent 는 객체 생성과 동시에 시스템에 유지되기 시작한다.
- PendingIntent.send() 는 일반적으로 직접 호출하지는 않는다. 해당 메서드는 PendingIntent 가 감싸고 있는 Intent 의 작업을 실행시키는 것인데, 바로 send() 해서 작업을 실행할 것이라면 굳이 PendingIntent 로 만들 이유가 없기 때문이다.
PendingIntent.Flags
- PendingIntent.CanceledException : 취소된 PendingIntent 를 실행하고자 할 때 발생하는 Exception 이다.
- PendingIntent.OnFinished : 전송이 올바르게 되었을 때 호출되는 Callback 인터페이스다.
- fun onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras)
- pendingIntent : 전달된 PendingIntent
- intent : PendingIntent 가 감싸고 있던 Intent
- resultCode : 전송에 의해 결정된 최종 결과 코드
- resultData : Broadcast에 의해 수집된 마지막 ResultData
- resultExtras : Broadcast에 의해 수집된 마지막 Extra 데이터
- fun onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras)
- PendingIntent.send() : PendingIntent 의 Intent 작업을 실행할 수 있다.
- PendingIntent.cancel() : 현재 활성화된 PendingIntent 를 취소할 수 있다. PendingIntent 를 소유한 원래 애플리케이션에서만 취소할 수 있다. 만약 취소 후에 send() 한다면 PendingIntent.CanceledException 발생한다.
- PendingIntent.getIntentSender() : PendingIntent 발신자에 대한 정보를 가진다. PendingIntent 를 생성한 애플리케이션의 package, UID 등 정보를 알 수 있다.
PendingIntent.Flags
- PendingIntent.FLAG_UPDATE_CURRENT : 시스템이 유지하고 있는 PendingIntent 가 있다면, 해당 PendingIntent 의 Extra data 를 업데이트한다.
- PendingIntnet.FLAG_CANCEL_CURRENT : 시스템이 유지하고 있는 PendingIntent 가 있다면 취소하고 다시 생성한다. 기존 Intent 의 Extra data 를 업데이트하고 싶을 때 사용할 수 있다. (like PendingIntent.FLAG_UPDATE_CURRENT)
- PendingIntent.FLAG_NO_CREATE : 시스템이 유지하고 있는 PendingIntent 가 없다면, 굳이 새롭게 생성하지 않는다. 수신하는 곳에서 Intent 가 null 이다.
- PendingIntent.FLAG_ONE_SHOT : PendingIntent 를 오직 한 번만 사용한다. 만약 동일한 PendingIntent 에 대해 재호출(e.g. PendingIntent.send() 두 번 호출)하면 예외가 발생한다.
- PendingIntent.FLAG_IMMUTABLE : PendingIntent.send() 에 intent 를 전달해도 값을 채울 수 없다. 공식문서에서는 해당 FLAG 사용을 강력히 권장한다. 그 이유는 다른 어플리케이션에서 PendingIntent 의 Intent 를 함부로 수정하지 못하게 함으로써 보안을 높일 수 있기 때문이다.
- PendingIntent.FLAG_MUTABLE : PendingIntent.send() 에 intent 를 전달하여 채워지지 않은 Extra Data 를 추가할 수 있다. API 31 이전에는 따로 설정하지 않으면 기본값이 MUTABLE 이다. Notification 의 RemoteInput(알림에서 답장), Bubble(도움말 풍선) 처럼 사용자 입력 텍스트를 intent 에 추가(= 수정)해야 하는 경우에만 사용해야 한다.
- 보안상의 이유로, Intent.setClass() 를 통해 항상 명시적 인텐트를 지정해주어야 한다.
'안드로이드 > Android' 카테고리의 다른 글
| [Android] JobScheduler, WorkManager (0) | 2025.04.29 |
|---|---|
| [Android] Service (0) | 2025.04.27 |
| [Android] MVI (0) | 2025.01.26 |