Android Note 2 Location to obtain geographic Location information (I)

Source: Internet
Author: User

 

On the fifth day of summer training in 2011, I would like to share with you my study on Android location. This is the most basic introduction to obtaining geographical location information, next time, I will introduce you to the location selection of more quality metrics Criteria. Today I will write a simple article and select two important SDK methods. I have translated them by myself, I also added my own understanding. I hope you can give me some advice. Let's get started! Obtain your location for tracking ~~~

Provide important geographic location information services

1 Location Manager Management Service

2 Location Provider provides the data content provider

 

Method 1: GPS features: high precision, high power consumption, no traffic permissions <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>

Method 2: NETWORK Features: Low Precision, power saving, NETWORK access required <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>

Or the permission <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>

Method 3: PASSIVE_PROVIDER has few materials. It is only used in specific scenarios. The SDK explains that it does not instantiate it to obtain the geographical location, but obtains the update location of other services or activities through getProvider, passively obtain updates.

 

 

Procedure:

1. Set permissions in the manifest. xml file.

2. Obtain LocaionManager

3. Select provider

4. Create a listener

 

 

/*************************************** **************************************** ***************************************/

Part 1 Permissions

Android security service mechanism. If an application wants to access local resources such as the contact list, dialing, GPS, or data of other applications, it must be licensed. Therefore, you need to add android: name = "android. permission. ACCESS_FINE_LOCATION"/> under the <manifest> label to use the geographic location information service for permission.

 

You can also click the permission label of the manifest. xml file to visually Add a license.

 

/*************************************** **************************************** ***************************************/

Part 2: Understanding LocationManager

 

The official SDK explains "This class provides access to the system location services. these services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

You do not instantiate this class directly; instead, retrieve it through Context. getSystemService (Context. LOCATION_SERVICE )."

The LocationManager class provides access to the system location services. These services allow applications to regularly update the device location, you can also initiate an intent for a specified activity when the device approaches a specified geographic location. You do not need to create a LocationManager instance. Instead, you can use Context. getSystemService (Context. LOCATION_SERVICE) to obtain the instance ."

 

Important Method: from the official SDK (attach my translation notes)

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Public Location getLastKnownLocation (String provider)

Since: API Level 1

 

Returns a Location indicating the data from the last known location fix obtained from the given provider. this can be done without starting the provider. note that this location cocould be out-of-date, for example if the device was turned off and moved to another location.

 

// Return a Location indicating the last known location obtained from the given provider, that is, the last obtained Location. This operation does not need to start the provider. Note that this address may have expired. For example, the device used may have been disabled or switched to another location.

 

If the provider is currently disabled, null is returned.

// If the current provider is disabled, the function returns null

 

Parameters

Provider the name of the provider

Returns

 

The last known location for the provider, or null

 

Throws

SecurityException if no suitable permission is present for the provider. // the permission mentioned earlier does not allow security exceptions.

IllegalArgumentException if provider is null or doesn't exist // The parameter is invalid, indicating that provider is null or does not exist.

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

Some Supplements:

Knowledge of Location class

Official SDK explanation: "A class representing a geographic location sensed at a particle time (a" fix "). A location consists of a latitude and longpolling, a UTC timestamp. and optionally information on altitude, speed, and bearing.

The general meaning is: "This class is used to indicate the geographic location information that is sensed at a specific time (we call it a fix // and we feel a little focused ), A location includes a longitude, latitude, a world timestamp, and optional information about the elevation, speed, and direction. "

You can use functions such as getLatitude () getlongdistance () getProvider () to obtain the latitude and longitude of the encapsulated information and the provider that provides the information. This is relatively simple.

 

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

Since: API Level 1

 

Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

// When registering the current activity, the provider is notified. It is equivalent to registering an event. Once in a while, the LocationListenner is called and the current location or status is updated.

 

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation (String) method.

// It may take a while to accept the latest location information. If you need to obtain the location information immediately, the program can use the getLastKonwnLocation method described above.

 

In case the provider is disabled by the user, updates will stop, and the onProviderDisabled (String) method will be called. as soon as the provider is enabled again, the onProviderEnabled (String) method will be called and location updates will start again.

 

// If the provider is stopped by the user (for example, GPS is disabled), the update will stop. In addition, the onProviderDisabled method (a method in the listener that needs to be rewritten) will be called. As long as the provider becomes available again, the onProviderEnable method will be called and the update operation starts immediately.

 

The frequency of notification may be controlled using the minTime and minDistance parameters. if minTime is greater than 0, the LocationManager cocould potentially rest for minTime milliseconds between location updates to conserve power. if minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. to obtain configurications as frequently as possible, set both parameters to 0.

// The frequency of notification (update) can be determined by using the minTime (minimum Update time unit: milliseconds) and minDistance (unit: meters) parameters. If the minTime is greater than 0, the LocationManager can take a rest in the minTime to save power consumption. If the minDistance is greater than 0, an update will be made every time this distance is changed. If you want to update the data as frequently as possible, set both parameters to 0.

 

Background services shoshould be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. in particle, values under 60000 ms are not recommended.

// For background services, you should note that setting a reasonable minTime will not consume too much power when the device keeps GPS or WIFI. MinTime value of MS is not recommended

 

The calling thread must be a logoff thread such as the main thread of the calling Activity.

// The thread that uses this method must enable the message loop when it is required. For example, the main thread of the called activity (refer to the Android thread mechanism. By default, the new thread does not enable the message loop, the main thread enables the message loop. For details, refer to the logoff class of the SDK)

Parameters

Provider the name of the provider with which to register

MinTime the minimum time interval for communications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

MinDistance the minimum distance interval for communications, in meters

Listener a {# link LocationListener} whose onLocationChanged (Location) method will be called for each location update // This is the event listener

 

Throws

IllegalArgumentException if provider or listener is null

RuntimeException if the calling thread has no logoff // runtime exception, run in the thread where the message loop is not enabled

SecurityException if no suitable permission is present for the provider.

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

The listener is not detailed. It is the basic knowledge of java. I believe everyone understands it ~

Well, the following is a piece of code I wrote. I used GPS to get the geographic location information and move it to listen for location changes.

Http://www.bkjia.com/kf/201111/110227.html

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.