1

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" />
Steve
  • 13
  • 3

1 Answers1

0

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.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Hmm ok, then should I use a Broadcast receiver to detect the Wifi switching? Or is there a better method? – Steve Sep 10 '17 at 11:37
  • Probably a broadcast receiver should be used. I don't have any experience on monitoring the Wi-Fi status myself. Apparently [NETWORK_STATE_CHANGED_ACTION](https://stackoverflow.com/questions/15761132/android-correct-way-to-detect-disconnecting-from-a-particular-wifi-ssid) and [SUPPLICANT_CONNECTION_CHANGE_ACTION](https://stackoverflow.com/questions/5888502/how-to-detect-when-wifi-connection-has-been-established-in-android) could be relevant events. – Markus Kauppinen Sep 10 '17 at 18:08