在Android上以对电池影响最小的方式获取用户位置

在Android上以对电池影响最小的方式获取用户位置,android,geolocation,android-location,Android,Geolocation,Android Location,我开始开发一个应用程序,它将永远留在后台,并检测用户何时在某个位置停留一段时间(然后显示通知,邀请用户打开应用程序以获取该位置的信息) 这与谷歌地图在餐馆里的做法非常相似,它会向你显示一个通知,让你检查有关它的评级 我想要的是对设备的影响最小,所以位置更新应该是非常“被动”的,只有在用户不移动时才能获取位置,如果可能的话,回收其他活动(可能是谷歌地图本身或设备上运行的其他位置应用程序)已经获取的位置数据。 这不是一个导航应用程序,所以我不需要实时精确的位置,只需要最精确、最省力的大致位置,并且只

我开始开发一个应用程序,它将永远留在后台,并检测用户何时在某个位置停留一段时间(然后显示通知,邀请用户打开应用程序以获取该位置的信息)

这与谷歌地图在餐馆里的做法非常相似,它会向你显示一个通知,让你检查有关它的评级

我想要的是对设备的影响最小,所以位置更新应该是非常“被动”的,只有在用户不移动时才能获取位置,如果可能的话,回收其他活动(可能是谷歌地图本身或设备上运行的其他位置应用程序)已经获取的位置数据。 这不是一个导航应用程序,所以我不需要实时精确的位置,只需要最精确、最省力的大致位置,并且只有在用户不移动的情况下


LocationListener
onLocationChanged
似乎是我的人,但我可以指定我不想触发设备的传感器,只在其他范围可用时重复使用位置数据吗?我的应用程序应该检查这些信息,然后决定在这些信息足够准确时进行反向地理编码。

是的,
LocationListener
onLocationChanged
是您的人,不过对于被动实施,您可以通过一些选项

首先,你可以检查最后一个已知的位置,也许可以比较它的时间;i、 并验证它是否对您有用

就代码而言,字面上…
具有与最后一个位置零件相关的内容:

  /**
     * Runs when a GoogleApiClient object successfully connects.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        // Provides a simple way of getting a device's location and is well suited for
        // applications that do not require a fine-grained location and that do not need location
        // updates. Gets the best and most recent location currently available, which may be null
        // in rare cases when a location is not available.
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
此外,您可以将它与对象组合,这有助于您的实现,因为您可以在尝试
getLastLocation()
后立即调用它,并且基本上有一个更可靠的获取位置的安排

     // Create the location request
            mLocationRequest = LocationRequest.create()
//priority object needs to be set, the following will definitely get results for you  
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)  

//interval updates can be on the lines of 15mins or 30... acc to your requirement
                            .setInterval(UPDATE_INTERVAL)  
                            .setFastestInterval(FASTEST_INTERVAL);
                    // Request location updates
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                            mLocationRequest, this);
我建议尝试一下,可以与
getLastLocation()
结合使用。这些电源模式专门用于优化电池/功耗和检索位置的效率

     // Create the location request
            mLocationRequest = LocationRequest.create()
//priority object needs to be set, the following will definitely get results for you  
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)  

//interval updates can be on the lines of 15mins or 30... acc to your requirement
                            .setInterval(UPDATE_INTERVAL)  
                            .setFastestInterval(FASTEST_INTERVAL);
                    // Request location updates
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                            mLocationRequest, this);