简体   繁体   English

Android-最佳位置追踪策略

[英]Android - Best Location Tracking Strategy

I am trying to write a location tracking service that starts as soon as the app is started, stops as soon as the app goes into the background, and restarts as soon as the app comes back to the foreground. 我正在尝试编写一个位置跟踪服务,该服务应在应用程序启动后立即启动,在应用程序进入后台后立即停止,并在应用程序回到前台后立即重新启动。

The service should poll for a new location every 5 minutes while it is running (to conserve battery) and when a new location is found (onLocationChanged()) it updates a variable that I can retrieve from any Activity. 该服务应在运行时每5分钟轮询一次新位置(以节省电池),当找到新位置时(onLocationChanged()),该服务将更新一个我可以从任何Activity中检索到的变量。

I have tried binding a service in my custom Application class but the service never is bound before my initial Activity loads - and my initial Activity requires this service so I keep getting a null pointer exception when trying to access the service. 我曾尝试在自定义Application类中绑定服务,但在加载初始Activity之前从未绑定该服务-并且我的初始Activity需要此服务,因此在尝试访问该服务时,我总是收到空指针异常。

But maybe I am going in the wrong direction - what would be the best strategy for this? 但是也许我走错了方向-最好的策略是什么? I don't need a super exact location and I don't care if it comes from the GPS or Network. 我不需要非常精确的位置,也不必担心它是否来自GPS或网络。

below code worked for me... 下面的代码为我工作...

you will get the location just use it wisely wherever you want... 您将获得该位置,只需在所需的任何地方明智地使用它即可。

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;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                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", "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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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