OngoingActivity

public final class OngoingActivity


Main class to access the Ongoing Activities API. It's created with the Builder. After it's created (and before building and posting the Notification) apply apply needs to be called:

{@code * NotificationCompat.Builder builder = new NotificationCompat.Builder(context).... * * OngoingActivity ongoingActivity = new OngoingActivity.Builder(context, notificationId, builder); * .... * ongoingActivity.apply(context); * * notificationManager.notify(notificationId, builder.build()); * }
Note that if a Notification with that id was previously posted it will be replaced. If you need more than one Notification with the same ID you can use a String tag to differentiate them in both the Builder and notify

Afterward, update can be used to update the status.

If saving the OngoingActivity instance is not convenient, it can be recovered (after the notification is posted) with recoverOngoingActivity

Summary

Nested types

OngoingActivity.Builder

Builder used to build an OngoingActivity

Public methods

@NonNull void
apply(@NonNull Context context)

Notify the system that this activity should be shown as an Ongoing Activity.

@Nullable Icon

Get the animated icon that can be used on some surfaces to represent this OngoingActivity.

@Nullable String

Get the Category of this OngoingActivity if set, otherwise the category of the corresponding notification.

@Nullable LocusIdCompat

Get the LocusId of this OngoingActivity, this can be used by the launcher to identify the corresponding launcher item and display it accordingly.

@NonNull int

Get the notificationId of the notification associated with this OngoingActivity.

@NonNull int

Get the id to this OngoingActivity.

@NonNull Icon

Get the static icon that can be used on some surfaces to represent this OngoingActivity.

@Nullable Status

Get the status of this ongoing activity, the status may be displayed on the UI to show progress of the Ongoing Activity.

@Nullable String

Get the tag of the notification associated with this OngoingActivity, or null if there is none.

@NonNull long

Get the time (in elapsedRealtime time) the OngoingActivity was built.

@Nullable String

Get the title of this OngoingActivity if set.

@NonNull PendingIntent

Get the intent to be used to go back to the activity when the user interacts with the Ongoing Activity in other surfaces (for example, taps the Icon on the WatchFace).

static @Nullable OngoingActivity

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance.

static @Nullable OngoingActivity

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance.

static @Nullable OngoingActivity
recoverOngoingActivity(
    @NonNull Context context,
    @NonNull int ongoingActivityId
)

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance.

@NonNull void
update(@NonNull Context context, @NonNull Status status)

Update the status of this Ongoing Activity.

Public methods

apply

@NonNull
public void apply(@NonNull Context context)

Notify the system that this activity should be shown as an Ongoing Activity. This will modify the notification builder associated with this Ongoing Activity, so needs to be called before building and posting that notification.

Parameters
@NonNull Context context

May be used to access system services. A reference will not be kept after this call returns.

getAnimatedIcon

@Nullable
@NonNull
public Icon getAnimatedIcon()

Get the animated icon that can be used on some surfaces to represent this OngoingActivity. For example, in the WatchFace.

getCategory

@Nullable
@NonNull
public String getCategory()

Get the Category of this OngoingActivity if set, otherwise the category of the corresponding notification.

getLocusId

@Nullable
@NonNull
public LocusIdCompat getLocusId()

Get the LocusId of this OngoingActivity, this can be used by the launcher to identify the corresponding launcher item and display it accordingly. If not set, returns the one in the corresponding Notification.

getNotificationId

@NonNull
public int getNotificationId()

Get the notificationId of the notification associated with this OngoingActivity.

getOngoingActivityId

@NonNull
public int getOngoingActivityId()

Get the id to this OngoingActivity. This id is used to reference it in recoverOngoingActivity

getStaticIcon

@NonNull
public Icon getStaticIcon()

Get the static icon that can be used on some surfaces to represent this OngoingActivity. For example in the WatchFace in ambient mode. If not set, returns the small icon of the corresponding Notification.

getStatus

@Nullable
@NonNull
public Status getStatus()

Get the status of this ongoing activity, the status may be displayed on the UI to show progress of the Ongoing Activity. If not set, returns the content text of the corresponding Notification.

getTag

@Nullable
@NonNull
public String getTag()

Get the tag of the notification associated with this OngoingActivity, or null if there is none.

getTimestamp

@NonNull
public long getTimestamp()

Get the time (in elapsedRealtime time) the OngoingActivity was built.

getTitle

@Nullable
@NonNull
public String getTitle()

Get the title of this OngoingActivity if set.

getTouchIntent

@NonNull
public PendingIntent getTouchIntent()

Get the intent to be used to go back to the activity when the user interacts with the Ongoing Activity in other surfaces (for example, taps the Icon on the WatchFace). If not set, returns the touch intent of the corresponding Notification.

recoverOngoingActivity

@Nullable
@NonNull
public static OngoingActivity recoverOngoingActivity(
    @NonNull Context context,
    @NonNull Predicate<@NonNull OngoingActivity> filter
)

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance.

Parameters
@NonNull Context context

May be used to access system services. A reference will not be kept after this call returns.

@NonNull Predicate<@NonNull OngoingActivity> filter

used to find the required OngoingActivity.

Returns
OngoingActivity

the Ongoing Activity or null if not found

recoverOngoingActivity

@Nullable
@NonNull
public static OngoingActivity recoverOngoingActivity(@NonNull Context context)

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance. Note that if there is more than one Ongoing Activity active you have not guarantee over which one you get, you need to use one of the other variations of this method.

Parameters
@NonNull Context context

May be used to access system services. A reference will not be kept after this call returns.

Returns
OngoingActivity

the Ongoing Activity or null if not found

recoverOngoingActivity

@Nullable
@NonNull
public static OngoingActivity recoverOngoingActivity(
    @NonNull Context context,
    @NonNull int ongoingActivityId
)

Convenience method for clients that don’t want to / can’t store the OngoingActivity instance.

Parameters
@NonNull Context context

May be used to access system services. A reference will not be kept after this call returns.

@NonNull int ongoingActivityId

the id of the Ongoing Activity to retrieve, set in setOngoingActivityId

Returns
OngoingActivity

the Ongoing Activity or null if not found

update

@NonNull
public void update(@NonNull Context context, @NonNull Status status)

Update the status of this Ongoing Activity. Note that this may post the notification updated with the new information.

Parameters
@NonNull Context context

May be used to access system services. A reference will not be kept after this call returns.

@NonNull Status status

The new status of this Ongoing Activity.