2013년 7월 3일 수요일

PendingIntent란?

다음 블로그에 정리가 잘 되어 있다.
http://huewu.blog.me/110084228131
간략히 하면,
Intent를 직접 보내지 않고 다른 클래스에게 Intent를 위임해주기 위한 클래스 정도 되겠다.
Notification Bar와 상호작용하는 어플리케이션을 작성할 때 널리 사용된다.

http://developer.android.com/reference/android/app/PendingIntent.html를 보면
A description of an Intent and target action to perform with it.라고 한다.
다음의 메소드들로 PendingIntent의 instance를 만들 수 있다.

LatinIME에서 다음과 같은 코드가 발견된다.

    /**
     * Arrange for the UploaderService to be run on a regular basis.
     *
     * Any existing scheduled invocation of UploaderService is removed and rescheduled.  This may
     * cause problems if this method is called often and frequent updates are required, but since
     * the user will likely be sleeping at some point, if the interval is less that the expected
     * sleep duration and this method is not called during that time, the service should be invoked
     * at some point.
     */
    public static void scheduleUploadingService(Context context) {
        final Intent intent = new Intent(context, UploaderService.class);
        final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
        final AlarmManager manager =
                (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent);
        manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                UploaderService.RUN_INTERVAL, UploaderService.RUN_INTERVAL, pendingIntent);
    }

getService를 통해 PendingIntent의 instance를 만들고 AlarmManager에 등록한다.
각 method의 parameters를 간단히 살펴보자

public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
  • context : The Context in which this PendingIntent should start the service.
  • requestCode : Private request code for the sender (currently not used).
  • intent : An Intent describing the service to be started.
  • 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.
public void setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
  • type : One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or RTC_WAKEUP.
  • triggerAtMillis : time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type). This is inexact: the alarm will not fire before this time, but there may be a delay of almost an entire alarm interval before the first invocation of the alarm.
  • intervalMillis : interval in milliseconds between subsequent repeats of the alarm. If this is one of INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY then the alarm will be phase-aligned with other alarms to reduce the number of wakeups. Otherwise, the alarm will be set as though the application had called setRepeating(int, long, long, PendingIntent).
  • operation : Action to perform when the alarm goes off; typically comes from IntentSender.getBroadcast().

댓글 없음: