I want to get the value of Longitude and Latitude of my current locatoin when offline and save the current location to its database. Is it possible to get the longitude and latitude of the device when mobile data and wifi are off but the GPS is ON?
Asked
Active
Viewed 1.3k times
8
-
1Yes you can. The normal location listener class itself will work. but you can't update those values to your servers or DB without internet. – Rakesh Polo Oct 24 '17 at 08:50
-
Hi @RakeshPolo, thanks for your comment, i just want to get the current/real-time location and save it on database – Oct 24 '17 at 08:53
-
1You can save it on your physical device's DataBase. – Rakesh Polo Oct 24 '17 at 08:55
-
yes, but i mean is it real time? like after a minute my location has change, the Latitude and Longitude will change without opening mobile connection or wifi but GPS is on – Oct 24 '17 at 08:56
1 Answers
16
Just use this code. Make sure the user hasn't selected wifi only or network only option in his location setings. it has to be high accuracy or GPS only. This piece of code will work.
public class Location extends AppCompatActivity {
LocationManager locationManager;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mContext=this;
locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
2000,
10, locationListenerGPS);
isLocationEnabled();
}
LocationListener locationListenerGPS=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
protected void onResume(){
super.onResume();
isLocationEnabled();
}
private void isLocationEnabled() {
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
alertDialog.setTitle("Enable Location");
alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
else{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
alertDialog.setTitle("Confirm Location");
alertDialog.setMessage("Your Location is enabled, please enjoy");
alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
}
}
The parameters of requestLocationUpdates methods are as follows:
provider: the name of the provider with which we would like to register. minTime: minimum time interval between location updates (in milliseconds). minDistance: minimum distance between location updates (in meters). listener: a LocationListener whose onLocationChanged(Location) method will be called for each location update.
Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Add above permissions to manifest file for the version lower than lollipop and for marshmallow and higher version use runtime permission.

Rakesh Polo
- 431
- 1
- 9
- 27
-
2thank-you so much... this code saved my day ! It was so simple, no need to get any service running or anything... !!! – mayur saini Jan 01 '19 at 07:10
-
1ive had to revert to using this because i need to get the location without a network. even though google docs says use fusedLocationClient. – filthy_wizard Apr 05 '20 at 00:06
-
Can i get location if i dont have Sim card on the device and even no internet? – Rahat Islam Khan Sep 22 '20 at 17:20
-