overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.main_activity)if(!hasGps()){Log.d(TAG,"This hardware doesn't have GPS.")// Fall back to functionality that doesn't use location or// warn the user that location function isn't available.}}privatefunhasGps():Boolean=packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)
Java
protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);if(!hasGps()){Log.d(TAG,"This hardware doesn't have GPS.");// Fall back to functionality that doesn't use location or// warn the user that location function isn't available.}...}privatebooleanhasGps(){returngetPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Detect location on Wear OS\n\nA watch's small, glanceable form factor makes Wear OS an ideal platform for apps\nthat record, report, and respond to user location. As examples, you can build\napps that give users real-time updates on their distance, speed, and direction,\nor provide glanceable cues about users' surroundings.\n\nFor more information, see [Build location-aware apps](/training/location).\n\nSome watches have a built-in GPS sensor that retrieves location data without\nrequiring a connected phone. When you request location data in a watch app, the\nsystem retrieves the location from either the phone or the watch using the most\npower-efficient method. So even without a GPS sensor in the watch, you can still\nget location information.\n\nTo reduce the effect of location data acquisition on battery life,\ncall\n[`setPriority()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder#setPriority(int))\nwith the value\n[`PRIORITY_BALANCED_POWER_ACCURACY`](https://developers.google.com/android/reference/com/google/android/gms/location/Priority#PRIORITY_BALANCED_POWER_ACCURACY).\nDifferent priority settings may\n[optimize chips differently](/guide/topics/location/battery#accuracy).\n\nWhen possible, conserve battery by asking for location no more\nthan once per minute using\n[`setInterval()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder#public-locationrequest.builder-setintervalmillis-long-intervalmillis).\n\nAs described in the following sections, your app needs to [handle the loss of\nlocation data](#notFound) when a watch without a sensor becomes disconnected from a phone.\n\nChoose your method\n------------------\n\nThere are a couple of ways to provide location data to a Wear OS app. You can\nuse the [Fused Location Provider (FLP)](#fused) or\n[Wear Health Services (WHS)](/training/wearables/health-services). FLP is a\nGoogle Play services API.\n\nUse FLP in the following circumstances:\n\n- You want location data in the moment but not continuously, such as marking the location of a parked car.\n- You want location continuously but don't need the location history.\n\nUse WHS in the following circumstances:\n\n- You want data from other sensors or are likely to want data from other sensors in the future.\n- Your app is a workout or exercise app that needs to track location data over the course of a specific time interval.\n\n| **Note:** We recommend WHS for all workout tracking, as it is more power efficient.\n\nFor watches paired with iPhones, see\n[Location data for watches paired to iPhones](/training/wearables/apps/independent-vs-dependent#location-data-for-watches-paired-to-iphones).\n\nUse the Fused Location Provider\n-------------------------------\n\nOn a watch, get location data using the\n[`FusedLocationProviderClient`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient).\nThe FLP may use location data from the phone. For more information, see\n[Create location services client](/training/location/retrieve-current#play-services).\n\nFor information about requesting location updates and continuously tracking a\nuser's location, see [Request location updates](/training/location/request-updates).\n| **Note:** When creating a `LocationRequest`, it's important to consider batching using the [`setMaxWaitTime()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest#public-locationrequest-setmaxwaittime-long-millis) because it can consume less battery and give a more accurate location, depending on the device's hardware capabilities.\n\nDetect on-board GPS\n-------------------\n\nIf a user goes jogging with a watch that lacks a built-in GPS sensor and leaves\nthe paired phone behind, your watch app can't get location data through the\nconnected device. Detect this situation in your app and warn the user that\nlocation capabilities are unavailable.\n\nTo determine whether a watch has a built-in GPS sensor, call the\n[`hasSystemFeature()`](/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String))\nmethod with\n[`PackageManager.FEATURE_LOCATION_GPS`](/reference/android/content/pm/PackageManager#FEATURE_LOCATION_GPS).\nThe following code detects whether the watch has a built-in GPS sensor when you start an activity: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.main_activity)\n\n if (!hasGps()) {\n Log.d(TAG, \"This hardware doesn't have GPS.\")\n // Fall back to functionality that doesn't use location or\n // warn the user that location function isn't available.\n }\n}\n\nprivate fun hasGps(): Boolean =\n packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)\n```\n\n### Java\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.main_activity);\n\n if (!hasGps()) {\n Log.d(TAG, \"This hardware doesn't have GPS.\");\n // Fall back to functionality that doesn't use location or\n // warn the user that location function isn't available.\n }\n ...\n}\n\nprivate boolean hasGps() {\n return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);\n}\n```\n\nHandle disconnection events\n---------------------------\n\nIf a watch has no built-in GPS sensor and loses connection to a phone, the watch\nloses its location data stream. If your app expects a constant stream of data,\nyour app must detect the loss of a connection, warn the user, and gracefully\ndegrade in functionality.\n\nAs with a mobile device, when you request location updates using\n[`FusedLocationProviderClient.requestLocationUpdates()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient#requestLocationUpdates(com.google.android.gms.location.LocationRequest,%20android.app.PendingIntent)), you pass in either a\n[`LocationCallback`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationCallback#public-void-onlocationavailability-locationavailability-locationavailability) or a\n[`PendingIntent`](https://developer.android.com/reference/android/app/PendingIntent).\nBoth of these include the location information and the\n[`LocationAvailability`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationAvailability)\nstatus.\n\nWhen using the `LocationCallback` option, override\n[`onLocationAvailability()`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationCallback#public-void-onlocationavailability-locationavailability-locationavailability) to receive updates regarding location availability status.\n\nWhen using the `PendingIntent` option and an\n[`Intent`](/reference/android/content/Intent) is returned, extract the location\navailability status from the `Intent` using the\n[`LocationAvailability.extractLocationAvailability(Intent)`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationAvailability#extractLocationAvailability(android.content.Intent))\nmethod.\n\nHandle location not found\n-------------------------\n\nWhen the GPS signal is lost, you can retrieve the last known location of the\nuser's watch. Retrieving the last known location is helpful when you can't get a\nGPS fix and when the watch lacks built-in GPS and loses its connection with the\nphone. For more information, see\n[Get the last known location](/training/location/retrieve-current).\n\nFlush location with batched calls\n---------------------------------\n\nIf you are using batched calls, call\n[`flushLocations()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient#public-taskvoid-flushlocations)\nwhen the screen comes back on or returns from ambient mode to\nimmediately return any batched locations to all registered `LocationListeners`,\n`LocationCallbacks`, and `Pending Intents`.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Optimize location for battery](/develop/sensors-and-location/location/battery)\n- [Create a notification {:#notification}](/develop/ui/views/notifications/build-notification)\n- [Detect when users start or end an activity](/develop/sensors-and-location/location/transitions)"]]