> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 通过 XSAPI C 更新用户的 Presence

> 使用 XblPresenceSetPresenceAsync 更新 XBOX Live 用户在线 Presence 状态并通告当前游戏内活动的 Flat C 示例。

本主题提供设置用户在线 Presence 状态的示例代码。XBOX 服务 Rich Presence 提供了向其他玩家通告玩家当前活动的功能。

有关详细信息，请参阅 [Rich Presence 概述](/services/xbox-services/community/presence/live-presence-overview)。

## 更新用户的在线 Presence 状态

此示例代码设置用户的在线 Presence 状态。

#### Flat C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    HRESULT hr = XAsyncGetStatus(asyncBlock, false);
};

HRESULT hr = XblPresenceSetPresenceAsync(xboxLiveContext, true, nullptr, asyncBlock.get());
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}
```

有关详细信息，请参阅以下内容：

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XAsyncGetStatus](/reference/system/xasync/functions/xasyncgetstatus)
* [XblPresenceSetPresenceAsync](/reference/live/xsapi-c/presence_c/functions/xblpresencesetpresenceasync)

## 更新 Rich Presence 状态

#### Flat C API

```cpp theme={null}
XblPresenceRichPresenceIds ids{};
pal::strcpy(ids.scid, sizeof(ids.scid), scid);
ids.presenceId = "playingMap";
std::vector<const char*> tokenIds{ "CurrentMap" };
ids.presenceTokenIds = tokenIds.data();
ids.presenceTokenIdsCount = tokenIds.size();

HRESULT hr = XblPresenceSetPresenceAsync(xboxLiveContext, true, &ids, asyncBlock.get());
```

有关详细信息，请参阅以下内容：

* [XblPresenceRichPresenceIds](/reference/live/xsapi-c/presence_c/structs/xblpresencerichpresenceids)
* [XblPresenceSetPresenceAsync](/reference/live/xsapi-c/presence_c/functions/xblpresencesetpresenceasync)


## Related topics

- [Rich Presence 示例代码](/zh-CN/services/xbox-services/community/presence/how-to/live-presence-howto-nav.md)
- [操作指南](/zh-CN/services/xbox-services/community/presence/how-to/index.md)
- [通过 REST 更新用户的 Presence](/zh-CN/services/xbox-services/community/presence/how-to/live-updating-presence-rest.md)
- [XBOX 成就管理器 API 概述](/zh-CN/services/xbox-services/player-data/achievements/achievements-manager/live-achievements-manager-overview.md)
- [获取用户的 Presence](/zh-CN/services/xbox-services/community/presence/how-to/live-getting-a-users-presence.md)
