0

I want to build code which provide current location while user is offline . i created a code in which i use GPS provider but i give current location only while user is online. i want to get current and fresh location of user Here is code

 final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            lat=location.getLatitude();
            String mm = "";
            longitude=location.getLongitude();
            locationManager.removeUpdates(this);
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(lat, longitude, 1);
                Address obj = addresses.get(0);
                mm = obj.getCountryName();
                mm=mm+" , "+obj.getSubAdminArea();
                mm=mm+" , "+obj.getSubLocality();
                mm=mm+" , "+obj.getFeatureName();



                Log.v("IGA", "Address" + mm);


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






        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
       String[] permissions,
                                                int[] grantResults)

        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
qasim butt
  • 133
  • 11
  • This [link](https://stackoverflow.com/questions/6694391/android-get-current-location-of-user-without-using-gps-or-internet) is not the exact same like you need, but probably might be helpful. – Procrastinator Oct 09 '17 at 07:45
  • [How do I get the current GPS location programmatically in Android?](https://stackoverflow.com/a/15757944) – M-- May 11 '20 at 16:14

1 Answers1

1

Please define online/offline

I suggest you use: locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListenerGPS, null);

your updated code should be:

final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener=new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {

        lat=location.getLatitude();
        String mm = "";
        longitude=location.getLongitude();

        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, longitude, 1);
            Address obj = addresses.get(0);
            mm = obj.getCountryName();
            mm=mm+" , "+obj.getSubAdminArea();
            mm=mm+" , "+obj.getSubLocality();
            mm=mm+" , "+obj.getFeatureName();

            Log.v("IGA", "Address" + mm);

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

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};
if (ActivityCompat.checkSelfPermission(context,
    Manifest.permission.ACCESS_FINE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED 
    && 
    ActivityCompat.checkSelfPermission(context,
    Manifest.permission.ACCESS_COARSE_LOCATION) 
    != PackageManager.PERMISSION_GRANTED) {
    String[] permissions, int[] grantResults)

    return;
}
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListenerGPS, null);
Aks4125
  • 4,522
  • 4
  • 32
  • 48
Marius Razvan Varvarei
  • 1,331
  • 1
  • 17
  • 21