Wifi provider onLocationChanged is never called

I got a strange problem. I use wifi to find the user's location. I tested several phones; on some of them I was never able to get a location update. it seemed that the onLocationChanged method was never called. My code is attached below .. Any suggestions are appreciated !!!

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); String provider = locationManager.NETWORK_PROVIDER; Location l = locationManager.getLastKnownLocation(provider); updateWithNewLocation(l); locationManager.requestLocationUpdates(provider, 0, 0, locationListener); } private void updateWithNewLocation(Location location) { TextView myLocationText; myLocationText = (TextView)findViewById(R.id.myLocationText); String latLongString = "No location found"; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + "\nLong:" + lng; } myLocationText.setText("Your Current Position is:\n" + latLongString); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); Log.i("onLocationChanged", "onLocationChanged"); Toast.makeText(getApplicationContext(), "location changed", Toast.LENGTH_SHORT).show(); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} 

};

manifest

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> 
+6
source share
5 answers

I assume that phones on which you do not receive location updates do not have a proper data connection. The network provider needs to connect GSM / 3G or WiFi data in order to receive location corrections on Google servers using data from cellular / Wi-Fi phones. You can use this method to verify this.

 public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } return false; 

}

+2
source

Please make sure your onProviderDisabled method is not called immediately.

If you find that it is called as soon as your provider registers, it means that location services are disabled on your phone.

0
source

Now I suspect that Google geolocation services were in some areas. My application started working again without changing the code (apk from the Play Store).

0
source

I also ran into the same problem with NetworkProvider when testing in ICS (4.0), but it worked fine in older versions (2.2 and 2.3.3 that I tested). Here is the solution I found and works great in ICS . Use Criteria to get location updates as shown below:

  LocationManager locationManager= (LocationManager) <YourAct> .getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); //ACCURACY_COARSE or ACCURACY_FINE based on usage String provider = locationManager.getBestProvider(criteria, true); // Register the listener with the Location Manager to receive location // updates locationManager.requestLocationUpdates(provider, 0, 0, locationListener); 

Replace the code as above. This will help you get location updates.

0
source

I had the same problem and it turned out that onLocationChanged not being called if there is getLastKnownLocation that matches "current". This means that you will not get a new location because there is nothing new to report ... The solution is to use the last known location, even if it is very old (and continue to listen to updates if you need a more accurate / new location (if such events)).

0
source

Source: https://habr.com/ru/post/918908/


All Articles