Reputation: 181
In one of my mobile application, I wanted to store real-time latitude and longitude of the user using GPS of Android device.
For that, I am going to use an API to send the value of latitude and longitude of device and will going to call that API every 10 seconds.
But, when the device is in idle mode and not moving anywhere, I don't want to send a request through API to avoid unnecessary load on the server.
Is there any method in Android Library that can detect that the device is moving or not? If the device is in pocket or I put it in the car in my purse, it must check that I am walking or sleeping or the car is moving or not.
I am a novice to Android.
Thanks JD
Upvotes: 3
Views: 1349
Reputation: 189
You can achieve this using Activity Recognition API in android. There are various activity to identify. https://developer.android.com/guide/topics/location/transitions
Upvotes: 2
Reputation: 23394
Save last sent latitude and longitude in shared prefs and calculate distance between last location and current location , If distance is more then update new location to server .
To calculate distance use this function
private float calculateLocationDifference() {
float[] dist = new float[1];
Location.distanceBetween(lastLocation.getLatitude(), lastLocation.getLongitude(), currentLocation.getLatitude(), currentLocation.getLongitude(), dist);
return dist[0];
}
Please do note that getting location continuously will drain the user battery faster .
Upvotes: 3