Android 遊戲中的事件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
隨著 Google Sign-In API 淘汰,我們將在 2026 年移除遊戲第 1 版 SDK。2025 年 2 月後,您將無法在 Google Play 上發布新整合 games v1 SDK 的遊戲。建議您改用 games v2 SDK。
雖然採用舊版遊戲第 1 版整合功能的現有遊戲仍可繼續運作幾年,但建議您從 2025 年 6 月開始遷移至第 2 版。
本指南適用於使用 Play 遊戲服務第 1 版 SDK。如需最新 SDK 版本的相關資訊,請參閱 v2 說明文件。
本指南說明如何使用 Google Play 遊戲服務提供的事件 API 收集玩家遊戲資料,以進行遊戲過程分析。此 API 位於 com.google.android.gms.games.event
和 com.google.android.gms.games
。
事前準備
建議您先複習事件遊戲概念,這會對您很有幫助。
開始使用事件 API 編寫程式碼之前:
取得事件用戶端
如要開始使用事件 API,遊戲必須先取得 EventsClient
物件。方法是呼叫 Games.getEventsClient()
方法,然後傳入活動和目前播放器的 GoogleSignInAccount
。如要瞭解如何擷取玩家帳戶資訊,請參閱「Android 遊戲登入」。
提交事件
您可以在遊戲中新增程式碼,以在遊戲發生特定事件時通知 Google Play 遊戲服務。
如要傳送事件更新,請使用 eventId
值以及等於或大於 0 的整數 incrementAmount
呼叫 EventsClient.increment()
。
- 首次在 Google Play 管理中心定義事件時,Google Play 遊戲服務會產生
eventId
,用於在遊戲中識別此事件。
- 您可以使用
incrementAmount
輸入指定玩家完成特定遊戲目標的量化進度。舉例來說,如果遊戲要追蹤的事件是「打敗 500 隻大眼怪」,incrementAmount
值可以是玩家在單次戰鬥中殺死的怪物數量。
以下是如何以遞增值為 1 提交事件的範例:
public void submitEvent(String eventId) {
Games.getEventsClient(this, GoogleSignIn.getLastSignedInAccount(this))
.increment(eventId, 1);
}
擷取事件
您可以呼叫 EventsClient.load()
以擷取 Google 伺服器儲存的所有遊戲事件資料。在方法呼叫中,傳入布林值以表示 Google Play 遊戲服務是否應清除使用者裝置中的本機快取資料。
如要擷取在 Google Play 管理中心定義的特定事件資料,請呼叫 EventsClient.loadByIds()
,然後傳入輸入參數的事件 ID 陣列。
以下程式碼片段說明如何查詢 Google Play 遊戲服務,以取得遊戲的所有事件清單:
public void loadEvents() {
Games.getEventsClient(this, GoogleSignIn.getLastSignedInAccount(this))
.load(true)
.addOnCompleteListener(new OnCompleteListener<AnnotatedData<EventBuffer>>() {
@Override
public void onComplete(@NonNull Task<AnnotatedData<EventBuffer>> task) {
if (task.isSuccessful()) {
// Process all the events.
for (Event event : task.getResult().get()) {
Log.d(TAG, "loaded event " + event.getName());
}
} else {
// Handle Error
Exception exception = task.getException();
int statusCode = CommonStatusCodes.DEVELOPER_ERROR;
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
statusCode = apiException.getStatusCode();
}
showError(statusCode);
}
}
});
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Events in Android games\n\nFollowing the deprecation of the\n[Google Sign-In](https://android-developers.googleblog.com/2024/09/streamlining-android-authentication-credential-manager-replaces-legacy-apis.html)\nAPI, we are removing the games v1 SDK in 2026. After February 2025, you will be unable to publish\ntitles that are newly integrated with games v1 SDK, on Google Play. We recommend that you use the\ngames v2 SDK instead. \n\nWhile existing titles with the previous games v1 integrations continue to function for a\ncouple of years, you are encouraged to\n[migrate to v2](/games/pgs/android/migrate-to-v2)\nstarting June 2025. \n\nThis guide is for using the Play Games Services v1 SDK. For information\non the latest SDK version, see the\n[v2 documentation](/games/pgs/android/events).\n\nThis guide shows you how to collect player gameplay data for game analytics using the events APIs\nprovided by Google Play Games Services. The APIs can be found in the\n[`com.google.android.gms.games.event`](https://developers.google.com/android/reference/com/google/android/gms/games/event/package-summary)\nand [`com.google.android.gms.games`](https://developers.google.com/android/reference/com/google/android/gms/games/package-summary).\n\nBefore you begin\n----------------\n\nIf you haven't already done so, you might find it helpful to review the\n[events game concepts](/games/pgs/events).\n\nBefore you start to code using the events APIs:\n\n- Define the events for your game in the [Google Play Console](https://play.google.com/apps/publish/).\n- Follow the [sign-in checklist recommendations](/games/pgs/v1/quality#sign-in).\n\nGet the events client\n---------------------\n\nTo start using the events APIs, your game must first obtain an\n[`EventsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient) object. You can do this by calling the\n[`Games.getEventsClient()`](https://developers.google.com/android/reference/com/google/android/gms/games/Games.html#getEventsClient(android.app.Activity,%20com.google.android.gms.auth.api.signin.GoogleSignInAccount)) method and passing in the\nactivity and the [`GoogleSignInAccount`](https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount) for the current player. To learn how to\nretrieve the player account information, see\n[Sign-in in Android Games](/games/pgs/v1/android/signin).\n| **Note:** The [`EventsClient`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient) class makes use of the Google Play services [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) class to return results asynchronously. To learn more about using tasks to manage threaded work, see the [Tasks API developer guide](https://developers.google.com/android/guides/tasks).\n\nSubmit events\n-------------\n\nYou can add code in your game to notify Google Play Games Services whenever an\nevent of interest to your game occurs.\n\nTo send an event update, call [`EventsClient.increment()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient.html#increment(java.lang.String,%20int)) with the `eventId` value and an\ninteger `incrementAmount` that is equal to or greater than 0.\n\n- The `eventId` is generated by Google Play Games Services when you first define the event in the Google Play Console and is used to uniquely identify this event in your game.\n- You can use the `incrementAmount` input to specify the player's quantitative progress towards completing some game-specific goal. For example, if the event your game wants to track is *'Defeat 500 bug-eyed monsters'* , the `incrementAmount` value can be the number of monsters that the player killed in a single battle.\n\nHere's an example of how to submit an event with an increment amount of 1: \n\n```text\npublic void submitEvent(String eventId) {\n Games.getEventsClient(this, GoogleSignIn.getLastSignedInAccount(this))\n .increment(eventId, 1);\n}\n```\n\nRetrieve events\n---------------\n\nYou can retrieve all events data stored in Google's servers for your game, by\ncalling [`EventsClient.load()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient.html#load(boolean)). In the\nmethod call, pass in a boolean value to indicate if Google Play Games Services should clear the locally\ncached data on the user's device.\n\nTo retrieve data for specific events that you defined in the Google Play Console, call\n[`EventsClient.loadByIds()`](https://developers.google.com/android/reference/com/google/android/gms/games/EventsClient.html#loadByIds(boolean,%20java.lang.String...)) and pass in an array of event IDs in the input parameters.\n\nThe following snippet shows how you can query Google Play Games Services for the\nlist of all events for your game: \n\n```gdscript\npublic void loadEvents() {\n Games.getEventsClient(this, GoogleSignIn.getLastSignedInAccount(this))\n .load(true)\n .addOnCompleteListener(new OnCompleteListener\u003cAnnotatedData\u003cEventBuffer\u003e\u003e() {\n @Override\n public void onComplete(@NonNull Task\u003cAnnotatedData\u003cEventBuffer\u003e\u003e task) {\n if (task.isSuccessful()) {\n // Process all the events.\n for (Event event : task.getResult().get()) {\n Log.d(TAG, \"loaded event \" + event.getName());\n }\n } else {\n // Handle Error\n Exception exception = task.getException();\n int statusCode = CommonStatusCodes.DEVELOPER_ERROR;\n if (exception instanceof ApiException) {\n ApiException apiException = (ApiException) exception;\n statusCode = apiException.getStatusCode();\n }\n showError(statusCode);\n }\n }\n });\n}\n```"]]