简体   繁体   中英

Detect if device is in motion in Android

How to detect in Android if device is moving or at 1 place?

Actually I don't want to insert location when device is at 1 place.

I tried using Accelerometer & activity recognition. But it doesn't help. Any good example will be helpful.

Used Fused Locaiton api in order to detect the speed of the device but Remember that never ever calculate the speed by positional change in time, just use the location.getSpeed() . The reason is that the GPS chip internally calculates the speed via physical doppler effect, not via positional change. While standing still, or at very low speeds this does not work well, so you have to filter out very low speeds, and bad gps signal. (via horicontal accuracy estimate)

If new location is within - let's say - 100m of old location, don't add it to database.

private static final float SIGNIFICANT_DISTANCE = 100f;

public void onLocationChanged(final Location newLocation) {
    if (newLocation != null) {
        final Location oldLocation = ; // Get last location from DB / field.
        if (oldLocation == null ) {
            // If this is first measurement
            // Add to DB.
        } else if (oldLocation.distanceTo(newLocation) > SIGNIFICANT_DISTANCE) {
            // If user has moved significantly
            // Add to DB.
        } else {
            // If user hasn't moved significantly
            // Ignore it or implement some sort of precision refinement logic and update entry in DB.
        }
    }
}

Additionally you'll want to enable listening to location only when Activity Recognition reports the user is on the move.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM