Android/Intent

Intent (4) - Pending(보류) intent

parkho79 2019. 2. 19. 11:30
728x90



Pending(보류) intent?

외부 애플리케이션에 내 권한을 허가해서 전달하는 intent.

Intent 를 하나 생성한 후, 특정 시점에 다른 component 들이 pending intent 를 사용하여 다른 component 에게 작업을 요청한다. 

 

예를 들어, 인터넷에서 음악을 다운로드 할 때 notification 에 다운로드 완료를 알리고 해당 알림을 클릭하면 다운로드가 완료된 음악 재생 activity 가 실행되는 것이 여기에 해당.

- 사용자가 Notification 으로 작업을 수행할 때 인텐트가 실행되도록 선언(Android 시스템의 NotificationManager가 인텐트를 실행).

- 사용자가 AppWidget 으로 작업을 수행할 때 인텐트가 실행되도록 선언(홈 스크린이 인텐트를 실행).

- 향후 지정된 시간에 인텐트가 실행되도록 선언(Android 시스템의 AlarmManager가 Intent를 실행).

 

 

 

Intent 생성

 

PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags);

 

◼ Type

- PendingIntent.getActivity(Context, int, Intent, int) : Activity를 시작하는 Intent 생성

- PendingIntent.getService(Context, int, Intent, int) : Service를 시작하는 Intent 생성 

- PendingIntent.getBroadcast(Context, int, Intent, int) : BroadcastReceiver를 시작하는 Intent 생성

 

◼ Parameters

- context : context 정보

- requestCode : Pending intent 를 가져올 때 구분하기 위한 id

- intent : 실행시키고 싶은 intent

- flag

 Flag Description 
 FLAG_CANCEL_CURRENT  이전에 생성한 PendingIntent 는 취소하고 새로 만듬 
 FLAG_NO_CREATE  이미 생성된 PendingIntent 가 없다면 null return.
 생성된 PendingIntent 가 있다면 해당 intent 반환 (재사용) 
 FLAG_ONE_SHOT  한번만 사용 (일회용) 
 FLAG_UPDATE_CURRENT  이미 생성된 PendingIntent 가 존재하면 해당 intnet 의 extra data 만 변경



Code

1. MainActivity



2. NotiActivity



위 예제는 하단 link 에서 확인할 수 있다.

https://github.com/parkho79/PendingIntentSample



728x90