Get current location (city name) without gps and mobile network












0















I have an android tablet that does not hasve a SIM card and GPS set to save battery mode too.
Tablet is connected to the internet by ethernet (with cable) and connected to LAN by WIFI.
I write a code for find current location (city name) and it work good in my phone. (GPS of my phone is active and connected to the internet by wifi (modem) or mobile network).



package com.xenon.location;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import static android.content.Context.LOCATION_SERVICE;

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;
private final Activity mActivity;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;
double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Activity activity) {
this.mContext = activity.getBaseContext();
this.mActivity = activity;
getLocation();
}

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

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
}else{
this.canGetLocation = true;
if (isNetworkEnabled) {
if (ContextCompat.checkSelfPermission(
mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity
, new String{android.Manifest.permission.ACCESS_COARSE_LOCATION}
, 0);
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

public void stopUsingGPS() {
if (locationManager != null) {
if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(mActivity, new String{android.Manifest.permission.ACCESS_COARSE_LOCATION},
0);
locationManager.removeUpdates(GPSTracker.this);
}
}
}

public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}

public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}

public String getCityName() {
String result = "";
if (location != null) {
double latitude, longitude;
List<Address> list;
Locale locale = new Locale("tr");
Geocoder geocoder = new Geocoder(mContext, locale);
try {
list = geocoder.getFromLocation(getLatitude(), getLongitude(), 2);

Address address = list.get(0);

/*
String gpsMsg = "CountryCode: " + address.getCountryCode() +
" ,AdminArea : " + address.getAdminArea() +
" ,CountryName : " + address.getCountryName() +
" ,SubLocality : " + address.getSubLocality();
*/
result = address.getAdminArea();

} catch (IOException e) {
e.printStackTrace();
}

catch (Exception e){

}
}
return result;
}

public boolean canGetLocation() {
return this.canGetLocation;
}

public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

alertDialog.setTitle("GPS is settings");

alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}


when run it on Tablet :



boolean isNetworkEnabled = false;
boolean canGetLocation = false;
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


isNetworkEnabled and canGetLocation always return false;










share|improve this question



























    0















    I have an android tablet that does not hasve a SIM card and GPS set to save battery mode too.
    Tablet is connected to the internet by ethernet (with cable) and connected to LAN by WIFI.
    I write a code for find current location (city name) and it work good in my phone. (GPS of my phone is active and connected to the internet by wifi (modem) or mobile network).



    package com.xenon.location;
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AlertDialog;

    import java.io.IOException;
    import java.util.List;
    import java.util.Locale;

    import static android.content.Context.LOCATION_SERVICE;

    public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    private final Activity mActivity;

    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location;
    double latitude;
    double longitude;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

    protected LocationManager locationManager;

    public GPSTracker(Activity activity) {
    this.mContext = activity.getBaseContext();
    this.mActivity = activity;
    getLocation();
    }

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

    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!isGPSEnabled && !isNetworkEnabled) {
    // no network provider is enabled
    }else{
    this.canGetLocation = true;
    if (isNetworkEnabled) {
    if (ContextCompat.checkSelfPermission(
    mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(mActivity
    , new String{android.Manifest.permission.ACCESS_COARSE_LOCATION}
    , 0);
    }
    locationManager.requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER,
    MIN_TIME_BW_UPDATES,
    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null) {
    location = locationManager
    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    }
    }
    }
    if (isGPSEnabled) {
    if (location == null) {
    locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,
    MIN_TIME_BW_UPDATES,
    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null) {
    location = locationManager
    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    }
    }
    }
    }
    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    return location;
    }

    public void stopUsingGPS() {
    if (locationManager != null) {
    if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(mActivity, new String{android.Manifest.permission.ACCESS_COARSE_LOCATION},
    0);
    locationManager.removeUpdates(GPSTracker.this);
    }
    }
    }

    public double getLatitude() {
    if (location != null) {
    latitude = location.getLatitude();
    }
    return latitude;
    }

    public double getLongitude() {
    if (location != null) {
    longitude = location.getLongitude();
    }
    return longitude;
    }

    public String getCityName() {
    String result = "";
    if (location != null) {
    double latitude, longitude;
    List<Address> list;
    Locale locale = new Locale("tr");
    Geocoder geocoder = new Geocoder(mContext, locale);
    try {
    list = geocoder.getFromLocation(getLatitude(), getLongitude(), 2);

    Address address = list.get(0);

    /*
    String gpsMsg = "CountryCode: " + address.getCountryCode() +
    " ,AdminArea : " + address.getAdminArea() +
    " ,CountryName : " + address.getCountryName() +
    " ,SubLocality : " + address.getSubLocality();
    */
    result = address.getAdminArea();

    } catch (IOException e) {
    e.printStackTrace();
    }

    catch (Exception e){

    }
    }
    return result;
    }

    public boolean canGetLocation() {
    return this.canGetLocation;
    }

    public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    mContext.startActivity(intent);
    }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.cancel();
    }
    });

    alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }
    }


    when run it on Tablet :



    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;
    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


    isNetworkEnabled and canGetLocation always return false;










    share|improve this question

























      0












      0








      0








      I have an android tablet that does not hasve a SIM card and GPS set to save battery mode too.
      Tablet is connected to the internet by ethernet (with cable) and connected to LAN by WIFI.
      I write a code for find current location (city name) and it work good in my phone. (GPS of my phone is active and connected to the internet by wifi (modem) or mobile network).



      package com.xenon.location;
      import android.app.Activity;
      import android.app.Service;
      import android.content.Context;
      import android.content.DialogInterface;
      import android.content.Intent;
      import android.content.pm.PackageManager;
      import android.location.Address;
      import android.location.Geocoder;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.os.IBinder;
      import android.provider.Settings;
      import android.support.annotation.Nullable;
      import android.support.v4.app.ActivityCompat;
      import android.support.v4.content.ContextCompat;
      import android.support.v7.app.AlertDialog;

      import java.io.IOException;
      import java.util.List;
      import java.util.Locale;

      import static android.content.Context.LOCATION_SERVICE;

      public class GPSTracker extends Service implements LocationListener {

      private final Context mContext;
      private final Activity mActivity;

      boolean isGPSEnabled = false;
      boolean isNetworkEnabled = false;
      boolean canGetLocation = false;

      Location location;
      double latitude;
      double longitude;

      private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

      private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

      protected LocationManager locationManager;

      public GPSTracker(Activity activity) {
      this.mContext = activity.getBaseContext();
      this.mActivity = activity;
      getLocation();
      }

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

      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

      if (!isGPSEnabled && !isNetworkEnabled) {
      // no network provider is enabled
      }else{
      this.canGetLocation = true;
      if (isNetworkEnabled) {
      if (ContextCompat.checkSelfPermission(
      mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(mActivity
      , new String{android.Manifest.permission.ACCESS_COARSE_LOCATION}
      , 0);
      }
      locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER,
      MIN_TIME_BW_UPDATES,
      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      if (locationManager != null) {
      location = locationManager
      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      if (location != null) {
      latitude = location.getLatitude();
      longitude = location.getLongitude();
      }
      }
      }
      if (isGPSEnabled) {
      if (location == null) {
      locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER,
      MIN_TIME_BW_UPDATES,
      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      if (locationManager != null) {
      location = locationManager
      .getLastKnownLocation(LocationManager.GPS_PROVIDER);
      if (location != null) {
      latitude = location.getLatitude();
      longitude = location.getLongitude();
      }
      }
      }
      }
      }

      } catch (Exception e) {
      e.printStackTrace();
      }

      return location;
      }

      public void stopUsingGPS() {
      if (locationManager != null) {
      if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

      ActivityCompat.requestPermissions(mActivity, new String{android.Manifest.permission.ACCESS_COARSE_LOCATION},
      0);
      locationManager.removeUpdates(GPSTracker.this);
      }
      }
      }

      public double getLatitude() {
      if (location != null) {
      latitude = location.getLatitude();
      }
      return latitude;
      }

      public double getLongitude() {
      if (location != null) {
      longitude = location.getLongitude();
      }
      return longitude;
      }

      public String getCityName() {
      String result = "";
      if (location != null) {
      double latitude, longitude;
      List<Address> list;
      Locale locale = new Locale("tr");
      Geocoder geocoder = new Geocoder(mContext, locale);
      try {
      list = geocoder.getFromLocation(getLatitude(), getLongitude(), 2);

      Address address = list.get(0);

      /*
      String gpsMsg = "CountryCode: " + address.getCountryCode() +
      " ,AdminArea : " + address.getAdminArea() +
      " ,CountryName : " + address.getCountryName() +
      " ,SubLocality : " + address.getSubLocality();
      */
      result = address.getAdminArea();

      } catch (IOException e) {
      e.printStackTrace();
      }

      catch (Exception e){

      }
      }
      return result;
      }

      public boolean canGetLocation() {
      return this.canGetLocation;
      }

      public void showSettingsAlert() {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

      alertDialog.setTitle("GPS is settings");

      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

      alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      mContext.startActivity(intent);
      }
      });

      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
      dialog.cancel();
      }
      });

      alertDialog.show();
      }

      @Override
      public void onLocationChanged(Location location) {
      }

      @Override
      public void onProviderDisabled(String provider) {
      }

      @Override
      public void onProviderEnabled(String provider) {
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }

      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
      return null;
      }
      }


      when run it on Tablet :



      boolean isNetworkEnabled = false;
      boolean canGetLocation = false;
      locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


      isNetworkEnabled and canGetLocation always return false;










      share|improve this question














      I have an android tablet that does not hasve a SIM card and GPS set to save battery mode too.
      Tablet is connected to the internet by ethernet (with cable) and connected to LAN by WIFI.
      I write a code for find current location (city name) and it work good in my phone. (GPS of my phone is active and connected to the internet by wifi (modem) or mobile network).



      package com.xenon.location;
      import android.app.Activity;
      import android.app.Service;
      import android.content.Context;
      import android.content.DialogInterface;
      import android.content.Intent;
      import android.content.pm.PackageManager;
      import android.location.Address;
      import android.location.Geocoder;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.os.IBinder;
      import android.provider.Settings;
      import android.support.annotation.Nullable;
      import android.support.v4.app.ActivityCompat;
      import android.support.v4.content.ContextCompat;
      import android.support.v7.app.AlertDialog;

      import java.io.IOException;
      import java.util.List;
      import java.util.Locale;

      import static android.content.Context.LOCATION_SERVICE;

      public class GPSTracker extends Service implements LocationListener {

      private final Context mContext;
      private final Activity mActivity;

      boolean isGPSEnabled = false;
      boolean isNetworkEnabled = false;
      boolean canGetLocation = false;

      Location location;
      double latitude;
      double longitude;

      private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

      private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

      protected LocationManager locationManager;

      public GPSTracker(Activity activity) {
      this.mContext = activity.getBaseContext();
      this.mActivity = activity;
      getLocation();
      }

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

      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

      if (!isGPSEnabled && !isNetworkEnabled) {
      // no network provider is enabled
      }else{
      this.canGetLocation = true;
      if (isNetworkEnabled) {
      if (ContextCompat.checkSelfPermission(
      mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(mActivity
      , new String{android.Manifest.permission.ACCESS_COARSE_LOCATION}
      , 0);
      }
      locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER,
      MIN_TIME_BW_UPDATES,
      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      if (locationManager != null) {
      location = locationManager
      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      if (location != null) {
      latitude = location.getLatitude();
      longitude = location.getLongitude();
      }
      }
      }
      if (isGPSEnabled) {
      if (location == null) {
      locationManager.requestLocationUpdates(
      LocationManager.GPS_PROVIDER,
      MIN_TIME_BW_UPDATES,
      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      if (locationManager != null) {
      location = locationManager
      .getLastKnownLocation(LocationManager.GPS_PROVIDER);
      if (location != null) {
      latitude = location.getLatitude();
      longitude = location.getLongitude();
      }
      }
      }
      }
      }

      } catch (Exception e) {
      e.printStackTrace();
      }

      return location;
      }

      public void stopUsingGPS() {
      if (locationManager != null) {
      if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

      ActivityCompat.requestPermissions(mActivity, new String{android.Manifest.permission.ACCESS_COARSE_LOCATION},
      0);
      locationManager.removeUpdates(GPSTracker.this);
      }
      }
      }

      public double getLatitude() {
      if (location != null) {
      latitude = location.getLatitude();
      }
      return latitude;
      }

      public double getLongitude() {
      if (location != null) {
      longitude = location.getLongitude();
      }
      return longitude;
      }

      public String getCityName() {
      String result = "";
      if (location != null) {
      double latitude, longitude;
      List<Address> list;
      Locale locale = new Locale("tr");
      Geocoder geocoder = new Geocoder(mContext, locale);
      try {
      list = geocoder.getFromLocation(getLatitude(), getLongitude(), 2);

      Address address = list.get(0);

      /*
      String gpsMsg = "CountryCode: " + address.getCountryCode() +
      " ,AdminArea : " + address.getAdminArea() +
      " ,CountryName : " + address.getCountryName() +
      " ,SubLocality : " + address.getSubLocality();
      */
      result = address.getAdminArea();

      } catch (IOException e) {
      e.printStackTrace();
      }

      catch (Exception e){

      }
      }
      return result;
      }

      public boolean canGetLocation() {
      return this.canGetLocation;
      }

      public void showSettingsAlert() {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

      alertDialog.setTitle("GPS is settings");

      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

      alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      mContext.startActivity(intent);
      }
      });

      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
      dialog.cancel();
      }
      });

      alertDialog.show();
      }

      @Override
      public void onLocationChanged(Location location) {
      }

      @Override
      public void onProviderDisabled(String provider) {
      }

      @Override
      public void onProviderEnabled(String provider) {
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }

      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
      return null;
      }
      }


      when run it on Tablet :



      boolean isNetworkEnabled = false;
      boolean canGetLocation = false;
      locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


      isNetworkEnabled and canGetLocation always return false;







      dictionary networking gps location wifi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 9:23









      BabakBabak

      65




      65
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can't get location because you disable the GPS.
          but if you want to get your location without GPS, try to get the CELL INFORMATION(MNC MCC CELLID AND LAC) where your device Connected then try to USE API (unwiredlab.com) to convert that Cell INFO into location.






          share|improve this answer
























          • ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

            – Babak
            Nov 28 '18 at 10:25












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334837%2fget-current-location-city-name-without-gps-and-mobile-network%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can't get location because you disable the GPS.
          but if you want to get your location without GPS, try to get the CELL INFORMATION(MNC MCC CELLID AND LAC) where your device Connected then try to USE API (unwiredlab.com) to convert that Cell INFO into location.






          share|improve this answer
























          • ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

            – Babak
            Nov 28 '18 at 10:25
















          0














          You can't get location because you disable the GPS.
          but if you want to get your location without GPS, try to get the CELL INFORMATION(MNC MCC CELLID AND LAC) where your device Connected then try to USE API (unwiredlab.com) to convert that Cell INFO into location.






          share|improve this answer
























          • ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

            – Babak
            Nov 28 '18 at 10:25














          0












          0








          0







          You can't get location because you disable the GPS.
          but if you want to get your location without GPS, try to get the CELL INFORMATION(MNC MCC CELLID AND LAC) where your device Connected then try to USE API (unwiredlab.com) to convert that Cell INFO into location.






          share|improve this answer













          You can't get location because you disable the GPS.
          but if you want to get your location without GPS, try to get the CELL INFORMATION(MNC MCC CELLID AND LAC) where your device Connected then try to USE API (unwiredlab.com) to convert that Cell INFO into location.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 6:23









          jhayjhayjhayjhay

          778




          778













          • ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

            – Babak
            Nov 28 '18 at 10:25



















          • ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

            – Babak
            Nov 28 '18 at 10:25

















          ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

          – Babak
          Nov 28 '18 at 10:25





          ok. thanks. i wll research about "get the CELL INFORMATION(MNC MCC CELLID AND LAC)"

          – Babak
          Nov 28 '18 at 10:25




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334837%2fget-current-location-city-name-without-gps-and-mobile-network%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues