In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use the location manager in Android". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to use the location manager in Android" can help you solve the problem.
First, declare the appropriate permissions in Manifest
To get location updates, the first step is to declare the appropriate permissions in manifest. If you forget to declare the appropriate permissions, your application will report a security exception at run time. When you use the LocationManagement method, you need to set permissions ACCESS_CORASE_LOCATION or ACCESS_FINE_LOCATION, for example, if your application uses web-based information services, you need to declare N ACCESS_CORASE_LOATION permissions, and to get GPS requests you need to declare ACCESS_FINE_LOCATION permissions. It is worth noting that if you declare ACCESS_FINE_LOCATION permissions, you imply that you also declare ACCESS_CORASE_LOCATION permissions. If an application uses a web-based location-based information service, you need to declare Internet permissions.
Second, get a reference to location management
LocationManager is a main class through which you can make location-based services in android. The usage method is similar to other services, and the corresponding reference can be obtained by calling the getSystemService method. If your application wants to get location updates in the foreground (in Activity), you should execute the following statement in onCreate ().
LocationManager locationManager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE)
Third, select a location provider
When there is no request, most android power management can now obtain location updates through a variety of underlying technologies, which are abstracted as the application of the LocationProvider class. Location providers have different operating characteristics in terms of time, accuracy, cost, power consumption and so on. Often, a precise location provider like GPS takes longer to fix, rather than imprecise, such as a web-based location provider. After tradeoff, you must choose a particular location provider, or multiple providers, which depend on the customer requirements of your application. For example, a check-in service at a key point, for example, requires high-precision positioning, while a retail store locator can be satisfied with city-level modifications. The following code snippet requires the support of an GPS provider.
LocationProvider provider = locationManager.getProvider (LocationManager.GPS_PROVIDER)
You provide some input criteria, such as accuracy, power requirements, cost, etc., and let android determine the most appropriate location matching provider. The following code snippet requires a more precise location provider rather than cost. It is important to note that this standard does not help you solve any providers, and the return value may be empty. At this time, your application should be able to handle this situation very well.
/ / Retrieve a list of location providers that have fine accuracy, no monetary cost, etc Criteria criteria = new Criteria (); criteria.setAccuracy (Criteria.ACCURACY_FINE); criteria.setCostAllowed (false); String providerName = locManager.getBestProvider (criteria, true); / / If no suitable provider is found, null is returned. If (providerName! = null) {...}
4. Check whether the location provider is enabled
In the setup, some location providers such as GPS can be turned off. It is a good practice to check whether the location provider you want is open by calling the isProviderEnabled () method. If the location provider is turned off, you can enable the user to open it by starting Intent in the settings.
@ Override protected void onStart () {super.onStart (); / / This verification should be done during onStart () because the system calls / / this method when the user returns to the activity, which ensures the desired / / location provider is enabled each time the activity resumes from the stopped state. LocationManager locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); final boolean gpsEnabled = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER); if (! gpsEnabled) {/ / Build an alert dialog here that requests that the user enable / / the location services, then when the user clicks the "OK" button, / / call enableLocationSettings ()} private void enableLocationSettings () {Intent settingsIntent = new Intent (Settings.ACTION_LOCATION_SOURCE_SETTINGS) StartActivity (settingsIntent);} that's all for "how to use the location Manager in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.