2

I don't know why, but my varaiable isNetowrkEnabled always return true. It doesn't matter if internet on my device is enabled or no.

This is my GPSTracker class:

public class GPSTracker extends Service implements LocationListener{

        private final Context mContext;

        boolean isNetworkEnabled = false;    
        boolean canGetLocation = false;

        Location location; // location

        protected LocationManager locationManager;

        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }

    public Location getLocation() {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    System.out.println("Network enabled");
                } else {
                    System.out.println("Network disabled");
                }
            }   
    }

Do you know what may be wrong in this code?

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Koin Arab
  • 1,045
  • 3
  • 19
  • 35

2 Answers2

3

There is nothing wrong in your code, in all likelihood. Whether a provider is enabled is determined by the Location portion of the Settings app (or equivalent controls offered elsewhere by the device manufacturer, such as in an app widget). So long as the network provider is not disabled in Settings, isProviderEnabled(LocationManager.NETWORK_PROVIDER) will return true. The provider being enabled has nothing to do with whether the provider will work given your lack of network connection.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I thought that is enough if i disabled wi-fi and mobile data on my device buin fact i had to do this in location services. Thanks :) – Koin Arab Jan 17 '15 at 17:19
0

Try this ..

ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
    return true;
}
else
{
    return false;
}
default locale
  • 13,035
  • 13
  • 56
  • 62