检查用户是否保持在同一位置

时间:2014-09-25 09:00:00

标签: android location

我在使用对用户位置敏感的Android应用程序。

一旦用户长时间呆在某个位置(例如20分钟),我们就应该做相应的事情(设置最近的停车位的状态,最近的是#34;忙碌"),一旦他离开该位置设置最近的停车位的状态,最近为" free"。

现在我可以获取已注册LocationListener的用户位置,但我有两个问题:

1)如何跟踪用户位置以确定他是否在同一位置具有指定时间的样式?

我想创建一个Handler并运行一项任务来检查位置:

private Location mLastLocation;
private Location mCurrentLocation;

private Handler mHandler = new Handler();
private Runnable mLocationCalculateRunnable = new Runnable() {
    @Override
    public void run() {
        if (mCurrentLocation != null) {
            if (!userLocationChanged()) {

            } else {

            }
        }
        mHandler.postDelayed(mLocationCalculateRunnable,10000); //check every 10 seconds
    }

    private boolean userLocationChanged() {
        return getDistance(mCurrentLocation, mLastLocation) > 5;
    }
};
mHandler.postDelayed(mLocationCalculateRunnable,1000);


private void onLocationChanged(Location location) {
    mLastLocation = mCurrentLocation;
    mCurrentLocation = location;
}

但我不能继续检查位置和时间。

2)即使用户点击back键返回主屏幕,应用程序仍应运行,屏幕可能会启动,即使cpu可能会进入睡眠状态。

如果是这样,我可能无法获得新位置,也可能会停止处理程序。

我想知道我的位置检查想法是否正确以及如何让应用程序在任何适合的情况下运行?

顺便说一句,我无法访问谷歌相关服务。

1 个答案:

答案 0 :(得分:1)

1)使用define为LocationManager设置正确的更新。这些定义是更新用户位置所需的最短时间和距离。

locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

2)运行将在后台运行的Service

小代码示例:

public class GPSservice extends Service implements LocationListener {


    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 2;

    private static final long MIN_TIME_BW_UPDATES = 1000 * 1;

    double latitude, longitude;

    boolean isGPSEnabled = false;

    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location;

    protected LocationManager locationManager;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        getLocation();

        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onLocationChanged(Location location) {
        new LocationReceiver(location, getApplicationContext());
    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                Log.d("Network", "OFF");
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "ON");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "ON");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }
}