android obtains location data and gps latitude and longitude 160 through gps; 160; 160; 160;
- 2020-05-24 06:06:00
- OfStack
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);
// Gets the name of the location service
String serviceName = this.LOCATION_SERVICE;
// Gets the managed object of the location service
LocationManager locationManager = (LocationManager)getSystemService(serviceName);
// through GPS Gets the location data for the location
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
updateToNewLocation(location);
/** The listener of the service management object */
// parameter 1 : the way of positioning parameter 2 : monitor the update interval (ms) parameter 3 : monitor the update distance (m) parameter 4 : method of monitoring
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 2000, 10, 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(" The latitude and longitude are: n"+" Dimensions: " + latitude+ "n longitude " + longitude);
} else {
tv1.setText(" Unable to obtain geographic information ");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}