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);