requestLocationUpdates generates location by wrong time - java

I'm trying to get locations from 2 different providers: network and GPS.
I'm using minTime 1 minute and minDistance 300 meters in requestLocationUpdates. With those parameters I don't expect to ever get more than one update per minute (per provider). The problem is, I am getting updates more frequently than that (more than 1 per minute). Why?
Here is some code to demonstrate:
mLocationManager.removeUpdates(listener);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEN_SECONDS*6, TEN_METERS*30, listener);
Here is the listener:
private final LocationListener listener = new LocationListener()
{
#Override
public void onLocationChanged(Location location)
{
if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER))
updateUILocation(location,LocationService.this.gpsLocation);
else
updateUILocation(LocationService.this.networkLocation, location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
UpdateUILocation:
private void updateUILocation(Location networkLocation,Location gpsLocation)
{
Location location = null;
if(gpsLocation == null || gpsLocation.getAccuracy() > 800)
{
if(!(networkLocation == null || networkLocation.getAccuracy() > 800))
location = networkLocation;
}
else
if(networkLocation == null)
{
if(gpsLocation.getAccuracy() < 800)
location = gpsLocation;
}
else
if(gpsLocation.getAccuracy() < networkLocation.getAccuracy() && gpsLocation.getAccuracy() < 500)
location = gpsLocation;
else
if(networkLocation.getAccuracy() < 800)
location = networkLocation;
if(location!=null)
{
if (mGeocoderAvailable)
doReverseGeocoding(location);
}
// Bypass reverse-geocoding only if the Geocoder service is available on the device.
}
The doReverseGeocoding turn the location into text and call the handler:
mHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == UPDATE_ADDRESS) // msg.what == 1
{
LocationService.this.address = (String) msg.obj;
new SendLocation(LocationService.this.id,(String)msg.obj); //send the location to the db
LocationService.this.gpsLocation = null; //reset gps value
LocationService.this.networkLocation = null; //reset network value
}
}
};
I tested this application while driving (which means that the minDistance parameter is not a factor) and I received more than 1 location update per minute.
Here are the locations I received while testing (please ignore the locations since it's in hebrew, just look for the time): http://imrip.interhost.co.il/

"The minTime is 1 minute and the minDistance is 300 meter. With those
parameters I never should get 2 locations in less then 1 minute..."
That's simply not true, not pre-JellyBean anyway.
http://developer.android.com/reference/android/location/LocationManager.html
Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.
Pre-JB you can get updates MORE FREQUENTLY than your min time specifies, especially if GPS reception is sketchy. See this answer for more details:
requestLocationUpdates interval in Android

Related

How can I cat latitude and longitude of Android device?

So I'm making this app which finds restaurants near you, fetching information from a food-delivery app, using JSoup library.
The only problem with it is that sometimes the latitude and the longitude are getting null value.
Situations in which my application is working:
-turning on GPS and the waiting at least 1-2 minutes;
-opening google maps, closing it, and then returning to the application;
So the main problem: I can't fetch the location right after I enable it and hit the 'Find restaurants' button, I need to wait 1-2 minutes after enabling location, then it's working.
private TextView result;
private FusedLocationProviderClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
client = LocationServices.getFusedLocationProviderClient(this);
getBtn = findViewById(R.id.getRestaurants);
result = findViewById(R.id.restaurantsList);
getBtn.setOnClickListener(this);
}
private void requestPermission(){
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1 );
}
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
client.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
result.setText("Getting location...");
if(location != null){
double latitude = getLat(location);
double longitude = getLng(location);
result.setText("Finding restaurants near you...");
getWebsite(latitude, longitude);
}else{
result.setText("Couldn't fetch location!");
}
}
});
Here is a good way to implement the FusedLocationProvider in Kotlin (you might adapt to Java or use Java and Kotlin side by side) :
private fun startLoc() {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
try {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
//showDialog(this#MapsActivity, TAG, "last know loc = ${location?.latitude} + ${location?.longitude}")
if (location != null){
lastKnowLoc = LatLng(location.latitude, location.longitude)
addMarkerToLocation(lastKnowLoc)
}
}
val locationRequest = LocationRequest()
locationRequest.interval = 10000
locationRequest.fastestInterval = 10000
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations){
//showDialog(this#MapsActivity, "locationResult", "location=${location.latitude};${location.longitude}")
addMarkerToLocation(LatLng(location.latitude, location.longitude))
val speed = location.speed
updateCamera(location.bearing)
}
}
}
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
btnStartLoc.isEnabled = false
btnStopLoc.isEnabled = true
}catch (e:SecurityException){}
}
you must use requestLocationUpdates()
you are using getLastLocation() and when the GPS is off and turned on after the last known location becomes null so you must call requestLocationUpdates()
you can find more information in the below link
https://developer.android.com/training/location/receive-location-updates

How can I get greater than 1m position accuracy with android FusedLocationProviderClient?

I'm trying to create an android app that gives accurate location (1m or less) indoors. It seems the recommended way is to use the
a FusedLocationProviderClient
However the current horizontal accuracy I'm getting is between 5m and 15m for longitude and latitude while the vertical accuracy is often up to 50m out.
Not sure if it makes a difference but the the test device is an HTC U11?
I've updated google play service location com.google.android.gms:play-services-location:17.0.0
I've included android.permission.ACCESS_FINE_LOCATION in my app manifest and locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); in the location request.
I've got some code in there which turns the GPS on.
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(3 * 1000); // 3 seconds
locationRequest.setFastestInterval(1 * 1000); // 3 seconds
new GpsUtils(this).turnGPSOn(new GpsUtils.onGpsListener() {
#Override
public void gpsStatus(boolean isGPSEnable) {
// turn on GPS
isGPS = isGPSEnable;
}
});
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
if (location != null) {
wayLatitude = location.getLatitude();
wayLongitude = location.getLongitude();
Lat.setText(Double.toString(location.getLatitude()));
Longs.setText(Double.toString(location.getLongitude()));
Acc.setText(String.format("%.2f", location.getAccuracy()));
if(location.hasAltitude()) {
Alt.setText(String.format("%.2f",location.getAltitude() - 108));
VAcc.setText(String.format("%.2f",location.getVerticalAccuracyMeters()));
}
else {
Alt.setText("Lost");
VAcc.setText("Lost");
}
SendToBackend(wayLatitude,wayLongitude, location);
if (!isContinue && mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(locationCallback);
}
}
}
}
};
According to google it should be possible to get below 1m accuracy indoors. a link.
Thanks in advance :-)

Counting time in background service

I've made a running app and for that, I've made a Service class to update user location (removed unimportant code):
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, ILocationConstants {
private static final String TAG = LocationService.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
private LocationData data;
#Override
public void onCreate() {
super.onCreate();
data = new LocationData();
startForeground();
}
private void startForeground() {
//BUIDLING A NOTIFICATION
startForeground(101, notification);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
data = Parcels.unwrap(intent.getParcelableExtra(LOCATION_MESSAGE));
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
}
return START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} catch (SecurityException ex) {
} catch (Exception e) {
}
}
private void sendUpdates() {
data.millisecondTime = SystemClock.uptimeMillis() - data.startTime;
data.updateTime = data.timeBuff + data.millisecondTime;
data.seconds = (int) (data.updateTime / 1000);
data.minutes = data.seconds / 60;
data.hours = data.minutes / 60;
data.minutes = data.minutes % 60;
data.seconds = data.seconds % 60;
data.milliSeconds = (int) (data.updateTime % 1000);
Intent locationIntent = new Intent();
locationIntent.setAction(LOACTION_ACTION);
locationIntent.putExtra(LOCATION_MESSAGE, Parcels.wrap(data));
LocalBroadcastManager.getInstance(this).sendBroadcast(locationIntent);
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
#Override
public void onDestroy() {
stopLocationUpdates();
mGoogleApiClient.disconnect();
super.onDestroy();
}
#Override
public void onConnected(Bundle connectionHint) throws SecurityException {
Log.i(TAG, "Connected to GoogleApiClient");
if (data.mLastLocation == null) {
data.mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
sendUpdates();
}
startLocationUpdates();
}
#Override
public void onLocationChanged(Location location) {
sendLocationChanged(location);
Log.d(TAG, "onLocationChanged: " + location.getLatitude() + ", " + location.getLongitude());
sendUpdates();
}
public void sendLocationChanged(Location location) {
//SEND DATA TO THE SERVER
}
#Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
After each location update I'm also calculating elapsed time (I have to do this by adding, because user may pause the activity). The problem is when I'm running the app in my office time is exact (for example 35 minutes), but when I'm riding a bicycle time is too short (shows 15mins when actually about 30 minutes passed). For the time perpsective the only difference is that it's called more often (in office gps refreshes only each 10-20 seconds, but outside may be even each 2 seconds). The route and distance are fine - service is not killed or something like that. I'm also updating time in activity, but replacing it on each update with the broadcasted (by the service) time, but even removing this didn't fix the issue. Why is this happening?
As mentioned in the SystemClock documentation,
uptimeMillis() is counted in milliseconds since the system was booted.
This clock stops when the system enters deep sleep (CPU off, display
dark, device waiting for external input)
So that may be the reason why you get different times. On the other hand, there are elapsedRealtime() and elapsedRealtimeNanos() methods, which
return the time since the system was booted, and include deep sleep.
This clock is guaranteed to be monotonic, and continues to tick even
when the CPU is in power saving modes, so is the recommend basis for
general purpose interval timing
Try using one of them.
I would suggest using the time you get with each location update in onLocationChanged() and passing it to your sendUpdates() method
void onLocationChanged(Location location){
long currentTime=location.getTime(); // unix time in milliseconds
.......
sendUpdates(currentTime);
}
then in sendUpdates(long currentTime) do the time calculations. If I understand correctly you are interested only in elapsed time since your app started. In that case I guess you can simplify your data object by having just data.startTime, data.seconds, data.minutes and data.hours for the elapsed time. You can obtain the start time from the location of the first fix after the app start (utilizing flag or similar to detect the first fix).
Then in sendUpdates(long currentTime) I would calculate the sec, min, hour of the elapsed time like this:
int elapsedSeconds=int((currentTime-data.StartTime)/1000);
data.hours=int(elapsedSeconds/3600);
data.minutes=int(elapsedSeconds/60)-60*data.hours;
data.seconds=elapsedSeconds-60*(data.minutes+60*data.hours);

Getting Current Location on Android - Currently crashing only on first app launch [Broadcast error]

So I am trying to add a some functionality to my app so that it can allow the user to get his location in a editbox instead of having to type it.
At the moment I have a "working example" but there is a strange bug in it: The first time you launch the app and click the button to get the location, the app crashes. It doesn't happen ever again, unless you reinstall the app and again it will crash only once ever. This seems to only be happening to Android 6.0 + devices (doesn't crash at all on 4.4 and 5.0).
Also sometimes it doesn't really get the location, just keeps returning null.
I suppose there is a better way to get Location, but this is my first time trying to get user's location so I don't know it.
So my question is essentially 2 questions - is there a better way to get location that will work on Android 4.4+ and why is my application crashing with pretty strange error.
The error:
system_process E/ActivityManager: Sending non-protected broadcast projekt.substratum.APP_CRASHED from system 820:system/1000 pkg android
java.lang.Throwable
at com.android.server.am.ActivityManagerService.checkBroadcastFromSystem(ActivityManagerService.java:17973)
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18545)
at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18636)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:881)
at com.android.server.am.AppErrors.crashApplicationInner(AppErrors.java:370)
at com.android.server.am.AppErrors.crashApplication(AppErrors.java:305)
at com.android.server.am.ActivityManagerService.handleApplicationCrashInner(ActivityManagerService.java:13554)
at com.android.server.am.ActivityManagerService.handleApplicationCrash(ActivityManagerService.java:13536)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1660)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2813)
at android.os.Binder.execTransact(Binder.java:565)
The Location class that I'm using (not written by me, only minor edits done)
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class ProviderLocationTracker implements LocationListener,LocationTracker {
// The minimum distance to change Updates in meters
private static final long MIN_UPDATE_DISTANCE = 10;
// The minimum time between updates in milliseconds
private static final long MIN_UPDATE_TIME = 1000 * 60;
private LocationManager lm;
public enum ProviderType{
NETWORK,
GPS
};
private String provider;
private Location lastLocation;
private long lastTime;
private boolean isRunning;
private LocationUpdateListener listener;
public ProviderLocationTracker(Context context, ProviderType type) {
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
if(type == ProviderType.NETWORK){
provider = LocationManager.NETWORK_PROVIDER;
}
else{
provider = LocationManager.GPS_PROVIDER;
}
provider = lm.getBestProvider(crit, true);
}
public String returnProvider(){
return provider;
}
public void start(){
if(isRunning){
//Already running, do nothing
return;
}
//The provider is on, so start getting updates. Update current location
isRunning = true;
lm.requestLocationUpdates(provider, MIN_UPDATE_TIME,MIN_UPDATE_DISTANCE, this);
lastLocation = null;
lastTime = 0;
return;
}
public void start(LocationUpdateListener update) {
start();
listener = update;
}
public void stop(){
if(isRunning){
lm.removeUpdates(this);
isRunning = false;
listener = null;
}
}
public boolean hasLocation(){
if(lastLocation == null){
return false;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return false; //stale
}
return true;
}
public boolean hasPossiblyStaleLocation(){
if(lastLocation != null){
return true;
}
return lm.getLastKnownLocation(provider)!= null;
}
public Location getLocation(){
if(lastLocation == null){
return null;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return null; //stale
}
return lastLocation;
}
public Location getPossiblyStaleLocation(){
if(lastLocation != null) {
return lastLocation;
}
return lm.getLastKnownLocation(provider);
}
public void onLocationChanged(Location newLoc) {
long now = System.currentTimeMillis();
if(listener != null){
listener.onUpdate(lastLocation, lastTime, newLoc, now);
}
lastLocation = newLoc;
lastTime = now;
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
Getting the location on button click listener:
if (checkLocationPermission()) {
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Request location updates:
getLocation();
}
}
getLocation method:
public void getLocation(){
//In case we havent started it somehow, just returns if we did
location.start();
double alti;
double longti;
if(location.hasLocation()){
if (location.getLocation() != null) {
alti = location.getLocation().getLatitude();
longti = location.getLocation().getLongitude();
locationText.setText(alti +","+longti);
}else{
locationText.setText("1Couldn't get location, check your GPS/NETWORK");
}
}else{
if (location.getPossiblyStaleLocation() != null) {
alti = location.getPossiblyStaleLocation().getLatitude();
longti = location.getPossiblyStaleLocation().getLongitude();
locationText.setText(alti + "," + longti);
}else{
locationText.setText("2Couldn't get location, check your GPS/NETWORK");
}
}
}
Solved the crash, as #Giovanni Terlingen suggested, it was caused by the theme engine that I had on my phone. The problem that remains is that the GPS location getting is not always working - For older phones it doesn't get the location via GPS but the Criteria assigns GPS as provider anyway.
Is there a better way to get locations for devices from KitKat to possibly Oreo?
This seems to only be happening to Android 6.0 + device
Since Android 6.0, permissions has been introduced. Make sure that your app has location access on Android 6.0+. You should inform the user that your want to access his/her location. See this useful link:
https://developer.android.com/training/permissions/requesting.html

Android requestLocationUpdates without minDistance

I have a need to get the gps location every 20 seconds or so, but ignoring the need to have the user move a certain number of meters, I tried setting the minDistance to zero but still no luck, its only when I manually send the location using the location control from DDMS that this gets activated and the location gets updated.
Note - This is run and tested from the emulator and not a real device, for now at least
private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journey);
Bundle dataBundle = this.getIntent().getExtras();
try {
driverData = new JSONObject(dataBundle.getString("driverData"));
} catch (JSONException e) {
e.printStackTrace();
}
//Set the UI elements into variables
journeyDescription = (TextView)findViewById(R.id.journeyDescription);
journeyText = (TextView)findViewById(R.id.journeyText);
confirmButton = (Button)findViewById(R.id.btnCheck);
//Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
try{
//Until requests are found keep broadcasting the drivers location and checking for requests
if(!foundRequest){
updateDriverLocationAndCheckForNewDriverRequests(location);
}
//Always keep updating the drivers location
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
}catch(JSONException e){
Toast.makeText(JourneyActivity.this, "An unexpected error occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Register the listener with the Location Manager to receive location updates can be either gps provider
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL_TIME, METERS_MOVED_VALUE, locationListener);
}
}
Here is the related code fragment, any ideas on how to trigger the listener without having the user move or me having to send information from the DDMS Location Controls woild be greatly appreciated
Regards,
Milinda
requestLocationUpdates takes milliseconds, not seconds.
You should change:
private final static Long INTERVAL_TIME = new Long(20);
private final static Float METERS_MOVED_VALUE = 0f;
to:
private final static int INTERVAL_TIME_SECONDS = 20 * 1000; // 20 seconds
private final static float INTERVAL_DISTANCE_METERS = 0f; // 0 meters
As far as testing on the emulator goes. I think your limited to manually pushing GPS locations or creating a few automated simulators:
Testing GPS in Android

Categories

Resources