如何在应用程序中内置GPS储物柜,如“ GPS Connected”?

问题描述 投票:1回答:1

当我使用]获取GPS位置时,我希望在我的应用上具有最高的精度>

Location.getLatitude()
Location.getLongitude()

而且我发现GPS必须在后台运行才能获得最高的精度。

[如果有人知道,GPS Connected应用程序以及GPS Fix,GPS Locker等类似应用程序的功能完全可以满足我的要求。

我想在我的应用程序中重现它,因此我不必安装和运行该额外的应用程序。

我为此找不到任何开源代码。

任何人都可以帮忙吗?

当我使用Location.getLatitude()Location.getLongitude()获取GPS位置,并且发现GPS必须在后台运行以...时,我希望在我的应用程序中具有最高的精度,]

经过长时间的搜索和来自多个来源的提示,我们发现某人正在做可能是我们想要的事情:

Getting the user's location in Android when the phone is locked

此人即使在屏幕锁定时也正在寻找一种解决方案来更新其GPS位置,但与此同时,我已经实施了我认为是我的问题的解决方案:启动服务,该服务通过功能getLocation()提取位置。该服务由可运行+处理程序在服务中定期调用,并以任意时间间隔UPDATE_CYCLE_PERIOD。

在getLocation()函数中,仅当连接了网络或GPS处于活动状态时才进行更新,而且还根据其他两个条件进行更新:MIN_TIME_BW_UPDATES和MIN_DISTANCE_CHANGE_FOR_UPDATES,由名为locationManager.requestLocationUpdates()的方法调用。我们仍然不确定确切在做什么,但是猜测是它只是在定义locationManager的更新条件。

在getLocation()内部调用的该特定方法的代码是:

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

定期运行+处理程序的代码:

    handler = new Handler();
    runnable = new Runnable() {
        public void run() {
           location = getLocation();
           handler.postDelayed(runnable, UPDATE_CYCLE_PERIOD);
        }
    };

该功能的代码是

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

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

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    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;
}

我们为这三个变量定义的数值是:

UPDATE_CYCLE_PERIOD = 5 sec (10 sec originally)

MIN_DISTANCE_CHANGE_FOR_UPDATES = 1 meter (10 meters originally)

MIN_TIME_BW_UPDATES = 1 sec  (1 min originally)

这是我们猜测,我们可以通过进行超频繁和超精确的更新来模仿应用程序“ GPS Connected”锁定GPS功能的效果-如果该应用程序确实在做类似的事情。由于这些值似乎非常消耗,我们可能必须测试和优化电池消耗。欢迎在这里发表任何评论。

java android gps android-gps
1个回答
0
投票

经过长时间的搜索和来自多个来源的提示,我们发现某人正在做可能是我们想要的事情:

© www.soinside.com 2019 - 2024. All rights reserved.