Reputation: 2115
I am using exact code form developer.android.com/ but it seems to not work 100%, I was same code yesterday and it worked. I have all permissions that are required.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I had added ACCESS_COARSE_LOCATION
permission just in case first doesn't work.
Code:
Log.i("DEV", "LocationManager");
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Log.i("DEV", "LocationListener");
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {Log.i("DEV", "onLocationChanged");}
public void onStatusChanged(String provider, int status, Bundle extras) {Log.i("DEV", "onStatusChanged");}
public void onProviderEnabled(String provider) {Log.i("DEV", "onProviderEnabled");}
public void onProviderDisabled(String provider) {Log.i("DEV", "onProviderDisabled");}
};
Log.i("DEV", "Register listener");
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
I have turned on mobile data or wifi always same log output:
09-28 20:06:56.816 21408-21408/smst.dev.mav.com I/DEV﹕ LocationManager
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ LocationListener
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ Register listener
09-28 20:06:56.826 21408-21408/smst.dev.mav.com I/DEV﹕ onProviderDisabled
Thanks!
Upvotes: 1
Views: 3016
Reputation: 3620
Android has two location providers - GPS and Coarse(Network) location. you need to enable both separately in order to use each one. take a look here:
you can check if each of them is enabled by using:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Upvotes: 1