Android programming access to GPS data detailed _android

Source: Internet
Author: User
Tags sqlite database stub

This article illustrates the Android programming approach to acquiring GPS data. Share to everyone for your reference, specific as follows:

GPS is an important part of the Android system, and it can be used to derive many position-related applications.

Android's GPS has a specialized management class called Locationmanager, and all GPS positioning services are generated and controlled by their objects.

The first thing to be clear is that the object acquisition of the Locationmanager class is not created directly, but is provided by the system, specifically by establishing an object reference to a Locationmanager object by using the following methods:

Copy Code code as follows:
Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service);

At this point, we can use Locationmanager this object of any GPS-related functions to operate. The following table lists several common member methods:

/tr> ,

method and its signature

description

list<string> getallproviders ()

gets the list of all the positioning modules associated with the device

String Getbestprovider" (Criteria, Boole A)

Get Set criteria (criteria object) the most appropriate device

gpsstatus getgpsstatus (gpsstatus)

get GPS current status

Location getlastknownlocation (String)

Gets the last available site information

Boolean isproviderenabled (S Tring)

determine if the device mentioned in the parameter is available


GPS also has a support API, location, which is an abstract class that represents the location information that can be used to get all the location data:

Method and its signature

Describe

Double Getaltitude ()

Get current height

Float getbearing ()

Get Current direction

Double Getlatitude ()

Get current latitude

Double Getlongitude ()

Get current longitude

Float GetSpeed ()

Get current speed


We can start with the above method of positioning.

You can pass the location information to a Location object:

Copy Code code as follows:
Locationlocation = Locationmanager.getlastknownlocation (Locationmanager.gps_provider);

We can also call the following function to do what we want for each updated location:

Copy Code code as follows:
Locationmanager.requestlocationupdates (locationmanager.gps_provider,1000, New Locationlistener ())

Where the first parameter is the Locationprovider object, the second parameter is the time difference of the refresh, which is set to 1 seconds, the third parameter is position error, this setting is 10 meters, the fourth parameter is a position listener object, it must implement 4 methods:

①. public void onlocationchanged (Location Location)
②. public void onproviderdisabled (String provider)
③. public void onproviderenabled (String provider)
④. public void onstatuschanged (String provider, int status, Bundleextras)

You can rewrite these methods to achieve our needs.

When we use the simulator for testing, because the emulator is unable to obtain the geographic location, it must be set with the emulator location controller:

The final result is as shown in the figure:

The code looks like this:

Package org.timm.android;
Import android.app.Activity;
Import Android.content.Context;
Import android.location.Location;
Import Android.location.LocationListener;
Import Android.location.LocationManager;
Import Android.os.Bundle;
Import Android.widget.EditText;
 public class Locationtryactivity extends activity {edittext text; /** called the activity is a.
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  Final Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service);
  Text = (edittext) Findviewbyid (r.id.textshow);
  Location Location = locationmanager.getlastknownlocation (Locationmanager.gps_provider);
  Showlocation (location); Locationmanager.requestlocationupdates (Locationmanager.gps_provider, 1000, new Locationlistener () {public void Onlocationchanged (Location Location) {//TODO auto-generated Method stub showlocation (location);
   The public void onproviderdisabled (String provider) {//TODO auto-generated the method stub showlocation (null); public void onproviderenabled (String provider) {//TODO auto-generated Method stub showlocation (Locationmana
   Ger.getlastknownlocation (provider)); 
  public void onstatuschanged (String provider, int status, Bundle extras) {//TODO auto-generated method stub}
 });
   public void Showlocation (Location currentlocation) {if (currentlocation!= null) {String s = "";
   S + + "current Location: (";
   S + + currentlocation.getlongitude ();
   S + + ",";
   S + + currentlocation.getlatitude ();
   s + = ") \ Speed:";
   S + + currentlocation.getspeed ();
   s + + "\ Direction:";
   S + + currentlocation.getbearing ();
  Text.settext (s);
  } else{Text.settext ("");

 }
 }
}

The last point to note is that you need to set permissions in Androidmanifest.xml:

Copy Code code as follows:
<uses-permissionandroid:name= "Android.permission.ACCESS_FINE_LOCATION"/>

PS: About Androidmanifest.xml details can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:

Http://tools.jb51.net/table/AndroidManifest

More interested readers of Android-related content can view the site: "Summary of Android Control usage", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced course" and " Android Resource Operating Skills summary

I hope this article will help you with the Android program.

Related Article

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.