Using the Location Manager

Source: Internet
Author: User

Before your application can begin processing location updates, it needs to perform some simple steps to set up access. In this lesson, you'll learn what these steps entail.

Declare proper permissions in Android manifest

The first step of setting up location update access is to declare proper permissions in the manifest. If permissions are missing, the application will get
SecurityExceptionAt runtime.

Depending onLocationManagerMethods used, either
ACCESS_COARSE_LOCATIONOrACCESS_FINE_LOCATIONPermission is needed. For example, you need to declare
ACCESS_COARSE_LOCATIONPermission if your application uses a network-based location provider only. The more accurate GPS requires
ACCESS_FINE_LOCATIONPermission. Note that declaring
ACCESS_FINE_LOCATIONPermission impliesACCESS_COARSE_LOCATIONAlready.

Also, if a network-based location provider is used in the application, you'll need to declare the Internet permission as well.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.INTERNET" />
Get a reference to locationmanager

LocationManagerIs the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling
getSystemService()Method. If your application intends to receive location updates in the foreground (within
Activity), You shoshould usually perform this step in
onCreate()Method.

LocationManager locationManager =        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Pick a location provider

While not required, most modern Android-powered devices can receive location updates through multiple underlying technologies, which are using acted to an application
LocationProviderObjects. location providers may have different performance characteristics in terms of time-to-fix, accuracy, monetary cost, power consumption, and so on. generally, a location provider with a greater accuracy, like
The GPS, requires a longer fix time than a less accurate one, such as a network-based location provider.

Depending on your application's use case, you have to choose a specific location provider, or multiple providers, based on similar tradeoffs. for example, a points of interest check-in application wowould require higher location accuracy than say, a retail
Store locator where a city level location fix wocould suffice. The snippet below asks for a provider backed by the GPS.

LocationProvider provider =        locationManager.getProvider(LocationManager.GPS_PROVIDER);

Alternatively, you can provide some input criteria such as accuracy, power requirement, monetary cost, and so on, and let Android decide a closest match location provider. the snippet below asks for a location provider with fine accuracy and no monetary
Cost. Note that the criteria may not resolve to any providers, in which case a null will be returned. Your application shocould be prepared to gracefully handle the situation.

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etcCriteria 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) {   ...}
Verify the location provider is enabled

Some location providers such as the GPS can be disabled in settings. It is good practice to check whether the desired location provider is currently enabled by calling
isProviderEnabled()Method. If the location provider is disabled, you can offer the user an opportunity to enable it in settings by firing
IntentWithACTION_LOCATION_SOURCE_SETTINGSAction.

@Overrideprotected 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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.