I am conducting test where we are compering GPS position of Android phone and GPS device which we would like to integrate in our hardware. But for the test to be accurate, phone need to use only GPS and not cell towers and WiFi.
Here is the code, where I set which service does the phone use.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_MILLISECONDS_BEFORE_UPDATE_LOCATION,
MIN_METERS_BEFORE_UPDATE_LOCATION,
new MyLocationListener(this));
So will phone use only GPS to get it's location? I can' turn WIFI off, because phones are sending current locations to our data base. Both GPS device and phone are on a fixed location at the time of test.
I know that there are already questions how to use GPS for acquiring location, but I would like to make sure, that phone is using just GPS.
It can find location by only using GPS (Assuming that location is reachable, for example not underground or something like that) By using internet it just finds location faster. Since your device is stationary you should be ok.
Read more about it here.
default it use gps and if gps not working it will use internet so you dont need wright something
Use the LocationManager.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.
来源:https://stackoverflow.com/questions/34721324/is-phone-using-only-gps-to-get-its-location