Snapshot API 總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Snapshot API 取得使用者目前環境的相關資訊。您可以使用 Snapshot API 存取各種情境信號:
- 偵測到使用者活動,例如走路或開車。
- 您已註冊的附近信標。
- 耳機狀態,是否已插入。
- 位置,包含經緯度。
系統會快取這些值,以便快速傳回。如果沒有資料,系統會執行感測和推論作業,以便傳回最新的狀態值。Awareness API 會針對具有公開 API 的內容類型傳回現有資料類型。
每個情境信號都有對應的 Snapshot API 方法。舉例來說,如要取得目前偵測到的活動,您可以呼叫 getDetectedActivity()
,使用 SuccessListener
取得 DetectedActivityResponse
,然後呼叫 getActivityRecognitionResult()
以傳回 ActivityRecognitionResult
,您可以從中取得實際要求資料。
以下範例說明如何取得最新偵測到的活動:
Awareness.getSnapshotClient(this).getDetectedActivity()
.addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
@Override
public void onSuccess(DetectedActivityResponse dar) {
ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
}
})
如要進一步瞭解如何使用要求資料,請參閱「取得快照資料」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Places and Weather contextual signals within the Awareness API are deprecated and have been turned off, with existing implementations restricted until specified dates.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers should utilize the Places SDK for Android instead of the Places contextual signal for new implementations.\u003c/p\u003e\n"],["\u003cp\u003eGoogle does not offer alternative functionality for the Weather contextual signal, impacting existing implementations relying on it.\u003c/p\u003e\n"],["\u003cp\u003eThe Awareness API continues to provide other contextual signals through the Snapshot API, including user activity, nearby beacons, headphone state, and location.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can access these signals using corresponding Snapshot API methods and retrieve relevant data for their applications.\u003c/p\u003e\n"]]],[],null,["# Snapshot API overview\n\n**Deprecation notice: Places and Weather contextual signals** **Warning:**\n| The Places and Weather contextual signals, exposed by the\n| [`getPlaces()`](/android/reference/com/google/android/gms/awareness/SnapshotClient#getPlaces()), and\n| [`getWeather()`](/android/reference/com/google/android/gms/awareness/SnapshotClient#getWeather())\n| methods, were deprecated.\n|\n| - The Places contextual signal was turned off. Continued use of this signal was restricted to customers with existing implementations, through October 30, 2019. New implementations must use the [Places SDK for Android](/places/android-sdk) instead of the Places contextual signal.\n| - The Weather contextual signal has also been turned off. Continued use of this signal has been restricted to customers with existing implementations, through January 31, 2020. Google doesn't offer alternative functionality for the Weather contextual signal.\n\nYou can use the [Snapshot API](/android/reference/com/google/android/gms/awareness/Awareness#getSnapshotClient(android.app.Activity))\nto get information about the user's current environment. With the Snapshot API,\nyou can access a variety of [context signals](/awareness/overview#context-types):\n\n- Detected user activity, such as when they walk or drive.\n- Nearby beacons that you've registered.\n- Headphone state, plugged in or not.\n- Location, which includes latitude and longitude.\n\nThe system caches these values so that they can be returned quickly. If there's\nno data, sensing and inference are performed to return fresh state values.\nThe Awareness API returns the existing data type for context types that have a\npublic API.\n\nEach context signal has a corresponding Snapshot API method. For example, to\nget the current detected activity, you call\n[`getDetectedActivity()`](/android/reference/com/google/android/gms/awareness/SnapshotClient#getDetectedActivity()),\nuse a `SuccessListener` to get a [`DetectedActivityResponse`](/android/reference/com/google/android/gms/awareness/snapshot/DetectedActivityResponse),\nthen call [`getActivityRecognitionResult()`](/android/reference/com/google/android/gms/awareness/snapshot/DetectedActivityResponse#getActivityRecognitionResult())\nto return an [`ActivityRecognitionResult`](/android/reference/com/google/android/gms/location/ActivityRecognitionResult)\nfrom which you can get the actual request data.\n\nThe following example shows how to get the latest detected activity: \n\n Awareness.getSnapshotClient(this).getDetectedActivity()\n .addOnSuccessListener(new OnSuccessListener\u003cDetectedActivityResponse\u003e() {\n @Override\n public void onSuccess(DetectedActivityResponse dar) {\n ActivityRecognitionResult arr = dar.getActivityRecognitionResult();\n }\n })\n\nFor more information about what you can do with the request data, see\n[Get Snapshot data](/awareness/android-api/snapshot-get-data)."]]