Copy Code code as follows:
Package com.action.android_test;
Import android.location.Location;
Import Android.location.LocationListener;
Import Android.location.LocationManager;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.TextView;
public class Mainactivity extends activity {
Private Location Location=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get the name of the location service
String ServiceName = this. Location_service;
Get the management object for the location service
Locationmanager Locationmanager = (locationmanager) getsystemservice (serviceName);
Obtaining location data via GPS
Location = Locationmanager.getlastknownlocation (Locationmanager.gps_provider);
Updatetonewlocation (location);
Listener for/** Service management Object
Parameter 1: Positioning the way parameter 2: Listener update interval (ms) Parameter 3: Listening for updated distance (m) Parameter 4: Method of monitoring
Locationmanager.requestlocationupdates (Locationmanager.gps_provider, Watts, new Locationlistener () {
public void onstatuschanged (String provider, int status, Bundle extras) {
}
public void onproviderenabled (String provider) {
}
public void onproviderdisabled ( String provider) {
}
public void onlocationchanged (Location Location) {
Updatetonewlocation (location);
}
});
}
private void Updatetonewlocation (Location Location) {
TextView TV1;
TV1 = (TextView) This.findviewbyid (R.ID.TV1);
if (location!= null) {
Double latitude = location.getlatitude ();
Double longitude= location.getlongitude ();
Tv1.settext ("Latitude and longitude of: N" + "Dimension:" + latitude+ "N Longitude" + longitude);
} else {
Tv1.settext ("Inability to obtain geographic information");
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}