The gps function provided by android allows you to conveniently obtain location information. Because this is the gps used in the simulator again, the gps location information should be sent through ddms.
The main. xml file has only one textview node and is not provided. The following permissions are required to operate gps:
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
The specific operation code is as follows:
MainActivity. java
[Java] view plaincopyprint?
Package com. example. locationmanager;
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. view. Menu;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView TV _show;
// Locate the manager
Private LocationManager lm;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TV _show = (TextView) this. findViewById (R. id. TV _show );
// Obtain the positioning manager provided by the system
Lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
// Obtain the latest location information from gps
Location location = lm. getLastKnownLocation (LocationManager. GPS_PROVIDER );
// Use location to display information
UpdateView (location );
// Set the listener
Lm. requestLocationUpdates (LocationManager. GPS_PROVIDER, 3000, 8, new LocationListener (){
@ Override
Public void onStatusChanged (String provider, int status, Bundle extras ){
}
@ Override
Public void onProviderEnabled (String provider ){
// Update the location when gps is available
UpdateView (lm. getLastKnownLocation (provider ));
}
@ Override
Public void onProviderDisabled (String provider ){
UpdateView (null );
}
@ Override
Public void onLocationChanged (Location location ){
// Update the location when the gps positioning information changes'
UpdateView (location );
}
});
}
Private void updateView (Location location ){
If (location! = Null ){
StringBuffer sb = new StringBuffer ();
Sb. append ("implementation location information: \ n ");
Sb. append ("precision :");
Sb. append (location. getlongpolling ());
Sb. append ("\ n-dimensional :");
Sb. append (location. getLatitude ());
Sb. append ("\ n Height :");
Sb. append (location. getAltitude ());
Sb. append ("\ n speed :");
Sb. append (location. getSpeed ());
Sb. append ("\ n Direction :");
Sb. append (location. getBearing ());
TV _show.setText (sb. toString ());
} Else {
TV _show.setText ("");
}
}
}