Android GPS Positioning
GPS Positioning seems to be unavailable in the room. Today I made a GPS Positioning Demo, including the user's longitude, latitude, height, direction, moving speed, accuracy, and other information. Android provides a dedicated LocationManager class for the GPS function. The program cannot directly create a LocationManager instance, but is obtained through the getSystemService () method of Context.
For example:
// Create the LocationManager object LocationManager lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
The following program is simple. Only one EditText is used in the layout to display all data:
Instance Demo:
MainActivity. java
Package sn. qdj. localgpsdemo; 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;/*** GPS Positioning * @ author qingdujun **/public class MainActivity extends Activity {LocationManager lm; EditText show; @ Override protected void onCr Eate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); show = (EditText) findViewById (R. id. show); // create the LocationManager object lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE); // obtain the latest Location information from GPS. Location lc = lm. getLastKnownLocation (LocationManager. GPS_PROVIDER); // update and display the positioning information updateView (lc); // you can obtain the GPS positioning information every 3 seconds. requestLocationUpdates (Lo CationManager. GPS_PROVIDER, 3000, 8, new LocationListener () {@ Overridepublic void onStatusChanged (String provider, int status, Bundle extras) {// TODO Auto-generated method stub} @ Overridepublic void onProviderEnabled (String provider) {// When GPS LocationProvider is available, update and locate updateView (lm. getLastKnownLocation (provider);} @ Overridepublic void onProviderDisabled (String provider) {// TODO Auto-generated metho D stubupdateView (null) ;}@ Overridepublic void onLocationChanged (Location location) {// when the GPS positioning information changes, update and locate updateView (location );}});} public void updateView (Location newLocation) {if (newLocation! = Null) {StringBuilder sb = new StringBuilder (); sb. append (Real-time location information :); sb. append (longitude :); sb. append (newLocation. getlongpolling (); sb. append (latitude :); sb. append (newLocation. getLatitude (); sb. append (height :); sb. append (newLocation. getAltitude (); sb. append (speed :); sb. append (newLocation. getSpeed (); sb. append (Direction :); sb. append (newLocation. getBearing (); sb. append (positioning precision :); sb. append (newLocation. getAccuracy (); show. setText (sb. toString ();} else {show. setText (null );}}}
Activity_main.xml
You need to add a permission to GPS positioning.