I have a Service that uses LocationListeners for both GPS and the Network providers.
The Listener for GPS detects when I disable or enable the GPS on my phone but the listener for Network (Wifi) doesn't detect the wifi going off/on. Below is my code for the Listener that handles GPS and Network. I don't get why I can only get "gps" in onProviderEnabled/onProviderDisabled.
Am I missing something?
@Override
public void onLocationChanged(Location location) {
if (Double.parseDouble(String.valueOf(location.getAccuracy())) > noiseAllowance) {
return;
}
if (provider.equals(LocationManager.GPS_PROVIDER)) {
gotPositionFromGps();
}
changeNotificationText(location);
processNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
//Here, when I disable Wifi on my phone, provider never returns wifi
if (new CheckAvailableProviders().checkGps(context))
return;
cleanResources();
}
@Override
public void onProviderEnabled(String provider) {
//Here, when I enable Wifi on my phone, provider never returns wifi
if (this.provider.equals(LocationManager.GPS_PROVIDER)
&& provider.equals(LocationManager.NETWORK_PROVIDER)
&& canEnableNetworkListener > 0) {
service.getLocationFromNetwork(60 * 1000, 15);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private void cleanResources() {
stopService();
clearVariables();
}
private void clearVariables() {
try {
countDown.cancel();
} catch (Exception e) {
}
countDown = null;
builder = null;
calculateHome = null;
context = null;
manageNotifications = null;
provider = null;
service = null;
}
private void stopService() {
Intent sendIntent = new Intent();
sendIntent.setAction("com.location.home.device.STOPPED_GETTING_LOCATION");
context.sendBroadcast(sendIntent);
context.stopService(new Intent(context, GpsService.class));
}
private void changeNotificationText(Location location) {
DecimalFormat precision = new DecimalFormat("0.000");
double lat = location.getLatitude();
double lon = location.getLongitude();
builder.setContentText(
context.getResources().getString(R.string.notification_text) +
String.valueOf(precision.format(lat)) +
" N " +
String.valueOf(precision.format(lon)) +
" E");
manageNotifications.notify(1, builder.build());
}
private void gotPositionFromGps() {
restartCountDown();
canEnableNetworkListener = 0;
service.removeListenerUpdates(1);
}
Manifest permissions:
<uses-permission android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.hardware.location.network"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
According to the documentation:
This provider determines location based on availability of cell tower and WiFi access points.
So, the Wi-Fi is not required if there's access to the cellular network and therefore the provider may not become disabled just by switching Wi-Fi off.