First initialize LocationManager,LocationListerner ,Location
---------------------------------------------------------------------------------------------------------
public class TestGPS extends Activity {
private LocationManager locationmanager;
private LocationListener locationlistener;
private Location location;
String lattitude, longitude;
Button btn_captured;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_gps);
btn_captured = (Button) findViewById(R.id.btn_captured);
locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER, 0,
0, new MyLocationListener());
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setCurrentLocation();
}
});
}
protected void setCurrentLocation() {
Location location = locationmanager
.getLastKnownLocation(locationmanager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(TestGPS.this, message, Toast.LENGTH_LONG)
.show();
btn_captured.setVisibility(View.VISIBLE);
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(AsdasdActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(TestGPS.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(TestGPS.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(TestGPS.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
---------------------------------------------------------------------------------------------------------
public class TestGPS extends Activity {
private LocationManager locationmanager;
private LocationListener locationlistener;
private Location location;
String lattitude, longitude;
Button btn_captured;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_gps);
btn_captured = (Button) findViewById(R.id.btn_captured);
locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER, 0,
0, new MyLocationListener());
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setCurrentLocation();
}
});
}
protected void setCurrentLocation() {
Location location = locationmanager
.getLastKnownLocation(locationmanager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(TestGPS.this, message, Toast.LENGTH_LONG)
.show();
btn_captured.setVisibility(View.VISIBLE);
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(AsdasdActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(TestGPS.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(TestGPS.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(TestGPS.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
0 comments: