Android的 - 最佳定位跟踪策略 [英] Android - Best Location Tracking Strategy

查看:157
本文介绍了Android的 - 最佳定位跟踪策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写,一旦应用程序开始启动的位置跟踪服务,停止一旦应用进入后台,并且应用程序又回到前台,尽快重新启动。

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.

在运行(以节约电池),并且在服务应轮询新的位置每5分钟的时候,一个新的位置被找到(onLocationChanged()),它更新,我可以从任何活动检索变量。

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.

我曾尝试绑定在我的自定义应用类服务,但该服务永远是我最初的活动加载之前束缚 - 和我最初的活动需要此服务,因此我不断收到试图访问服务时,一个空指针异常

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.

不过,也许我会在错误的方向 - 这将是我们的最佳策略是什么?我并不需要一个超级精确的位置,如果它来自GPS或网络我不在乎。

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.

推荐答案

低于code为我工作...

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;
}

这篇关于Android的 - 最佳定位跟踪策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆