Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onLocationChanged() never called

Tags:

I am writing a code that brings current location but it is not giving me the location because it never calls onLocationChanged() .Is there any way to find the location.

mobileLocation is coming 0 for this case, so the execution goes to the else block.

My code is

public class FindLocation { private LocationManager locManager; private LocationListener locListener; private Location mobileLocation; private String provider;  public FindLocation(Context ctx){     locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);     locListener = new LocationListener() {         @Override         public void onStatusChanged(String provider, int status,                 Bundle extras) {         }         @Override         public void onProviderEnabled(String provider) {         }         @Override         public void onProviderDisabled(String provider) {         }         @Override         public void onLocationChanged(Location location) {             System.out.println("mobile location is in listener="+location);             mobileLocation = location;         }     };     locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener);                   if (mobileLocation != null) {         locManager.removeUpdates(locListener);          String londitude = "Londitude: " + mobileLocation.getLongitude();         String latitude = "Latitude: " + mobileLocation.getLatitude();         String altitiude = "Altitiude: " + mobileLocation.getAltitude();         String accuracy = "Accuracy: " + mobileLocation.getAccuracy();         String time = "Time: " + mobileLocation.getTime();         Toast.makeText(ctx, "Latitude is = "+latitude +"Longitude is ="+londitude, Toast.LENGTH_LONG).show();     } else {         System.out.println("in find location 4");         Toast.makeText(ctx, "Sorry location is not determined", Toast.LENGTH_LONG).show();     } }    } 
like image 475
abhishek ameta Avatar asked Aug 13 '12 12:08

abhishek ameta


1 Answers

i run my app on real device . i use network instead of GPS and onLocationChanged is called:

locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null); 
like image 196
Gal Rom Avatar answered Nov 13 '22 07:11

Gal Rom