티스토리 뷰

Android/클래스

PendingIntent

이주성 2013. 6. 27. 09:06

Intent와 PendingIntent 이해하기

PendingIntent도 일종의 Intent라고 생각해도 된다. 그런데 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 객체냐에 따라서 그 객체가 activity를 실행시킬지 서비스를 수행할지, 방송을 실행시킬지가 결정된다는 것이다.

다음은 Intent에 대한 설명이다. 이 설명을 보면 PendingIntent와 개념상 매우 유사함을 볼수가 있다.
Three of the core components of an application - activities, services, and broadcast receivers - are activated through messages, called intents. Intent messaging is a facility for late run-time binding between components in the same or different applications. The intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed - or, often in the case of broadcasts,
a description of something that has happened and is being announced. There are separate mechanisms for delivering intents to each type of component:

ㆍAn Intent object is passed to Context.startActivity() or Activity.startActivityForResult()
   to launch an activity or get an existing activity to do something new.
   ⇒ 이건 Intent가 Activity를 실행하는 용도로 쓰인다는 뜻이다.

ㆍAn Intent object is passed to Context.startService() to initiate a service or deliver new
   instructions to an ongoing service. Similarly, an intent can be passed to
   Context.bindService() to establish a connection between the calling component and a
   target service.
   ⇒ 이건 Intent가 서비스를 실행하는 용도로 쓰인다는 뜻이다.

ㆍIntent objects passed to any of the broadcast methods (such as
   Context.sendBroadcast(), Context.sendOrderedBroadcast(), or
   Context.sendStickyBroadcast()) are delivered to all interested broadcast receivers.
   ⇒ 이건 Intent가 BR(방송)을 실행하는 용도로 쓰인다는 뜻이다.

이렇게 Intent가 Activity, Service, Broadcast Receiver를 수행하는 기능을 하듯이 PendingIntent도 PendingIntent 객체를 생성하는 세 개의 메소드가
getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)인데 이들 각각은 순서대로 activity를
실행, Broadcast Receiver를 실행, 서비스를 실행시키는 용도로 PendingIntent 객체가 생성된다는 것이다.

즉 PendingIntent 객체가 getBroadcast(Context, int, Intent, int)에 의해서 생성되었으면 그 PendigIntent 객체로는 방송을 실행한다는 것이고 getActivity(Context, int, Intent, int)에 의해서 생성된 PendingIntent 객체는 activity를 실행시킨다는 것이다. 어느 메소드에 의해서 객체가 생성되었느냐에 따라서 PendingIntent객체가 실행하는 컴포넌트가 달라진다는 것이다.

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

context : The Context in which this PendingIntent should perform the broadcast.
requestCode : Private request code for the sender (currently not used).
intent : The Intent to be broadcast.
flags : May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT,
        FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to
        control which unspecified parts of the intent that can be supplied when the actual
        send happens.
Returns : Returns an existing or new PendingIntent matching the given parameters. May
        return null only if FLAG_NO_CREATE has been supplied.

요약하면 Intent는 Activity, Service, BR을 실행시키는 message이다. PendingIntent도 일종의 Intent인데 이것의 객체가 getActivity()에 의해서 생성된 것이면 그 PendingIntent 객체는 Activity를 실행시키고 getBroadcast()에 의해서 생성된 객체이면 그 객체는 방송을 실행시킨다.

방송을 내 보낼때 어떤 컴포넌트에게 내보내는지는 Intent가 특정 컴포넌트를 호출하는 기능(명시적, 암시적 호출)이 있으므로 Intent의 이 기능을 PendingIntent가 활용해서 특정 컴포넌트에게 방송을 내보낸다.

이때 특정 컴포넌트가 방송을 수신할 수 있기 위해서는 Menifest에 방송 수신에 대한 정보를 기록해 두어야 한다.
그런데 이렇게 PendingIntent로 할때는 Menifest에 intent-filter를 통해서 action내용을 표기할 필요가 없는 것 같다.

BR을 수신할 자바 파일(컴포넌트) 이름만 명시해 두면 되는 것 같다. 다음과 같이

여기서 C22_AlaramReceiver.java가 호출 실행된다는 것이다.
C22_AlaramReceiver.java는 BroadcastReceiver를 상속받은 프로그램이다.

'Android > 클래스' 카테고리의 다른 글

[번역]자바 Enum의 10가지 예제  (0) 2013.07.18
AsyncTask  (0) 2013.02.14
byte연산  (0) 2012.11.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함