I am trying to retrieve my current location on the button click in an editbox using gps or network..i have tried all possible methods which i found in tutorials and older posts.
pleas check your manifest file.whether have you added those permissions or not.
See the updated answer:
protected void showCurrentLocation()
{
geocoder = new Geocoder(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
myLocationListener
);
timer = new Timer();
timer.schedule(new GetLastLocation(),20000);
}
class GetLastLocation extends TimerTask {
@Override
public void run() {
timer.cancel();
locationManager.removeUpdates(locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
System.out.println("loc.."+location);
if (location != null)
{
String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(ShowActivity.this, message,
Toast.LENGTH_LONG).show();
//acTextView.setText(message);
try {
List addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
System.out.println("my location .."+address.getAddressLine(0));
acTextView.setText(address.getAddressLine(0));
}
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
else
{
AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!");
alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{}});
alertbox1.show();
}
}
return;
}
}
/** The location listener. */
LocationListener myLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};