Cute Apple
본문 바로가기
개발/Android

[Android] PendingIntent 와 getBroadcast()

by 미댕댕 2022. 8. 11.

간단한 알람앱을 만드는 프로젝트를 진행을 하면서 BroadcastReceiver 를 사용하게 되었다

PendingIntent.getBroadcast(~~) 를 통해서 생성한 PendingIntent 가 무엇인지에 대해 알아보자

 

PendingIntent❓

  • Intent의 기본 개념은 특정 컴포넌트(Activity, Service, Broadcast Receiver, Content Provider)를 실행시키는 메시지
  • 역시 마찬가지로 PendingIntent도 Intent의 일종이므로 특정 컴포넌트를 실행시키는 기능
  • "특정 시점"에 자신이 아닌 다른 컴포넌트들이 펜딩인텐트를 사용하여 다른 컴포넌트에게 작업을 요청시키는 데 사용
  • 그런데 PendingIntent는 생성자가 없고 아래의 세 개의 메소드들에 의해서 객체가 생성
ㆍgetActivity(Context, int, Intent, int)
ㆍgetBroadcast(Context, int, Intent, int)
ㆍgetService(Context, int, Intent, int)
  • 어느 메소드에 의해서 객체가 생성되었느냐에 따라서 PendingIntent객체가 실행하는 컴포넌트가 달라진다는 것
  • 이때 특정 컴포넌트가 방송을 수신할 수 있기 위해서는 Menifest에 방송 수신에 대한 정보를 기록

 

사용하기

public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags) ⇒ Retrieve a PendingIntent that will perform a broadcast, like calling    Context.sendBroadcast().
- Android Developer Document

 

//AlarmReceiver 는 BR 을 상속받은 class
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this,
    ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    
//알람매니저로 알람 맞추기
// setInexactRepeating 의 경우 정확한 시간에 울리지는 않는다.
alarmManager.setInexactRepeating(
    AlarmManager.RTC_WAKEUP,
    calendar.timeInMillis,
    AlarmManager.INTERVAL_DAY,
    pendingIntent
)

 

 

참고

https://developer-joe.tistory.com/22

https://developer.android.com/reference/android/app/PendingIntent

반응형

'개발 > Android' 카테고리의 다른 글

[Android] Unsolved reference  (0) 2022.08.15
[Android] Coroutines vs Thread  (0) 2022.08.13
[Android] ExoPlayer2 사용기  (0) 2022.08.10
[Android] FragmentManager란?  (0) 2022.08.09
[Android] Vector 와 Bitmap  (0) 2022.08.08

댓글