【问题标题】:Is it possible to trigger location change without battery drain?是否可以在不消耗电池的情况下触发位置更改?
【发布时间】:2016-11-13 11:42:15
【问题描述】:

在我的应用程序中,我需要在进入另一个城市时获取用户位置的更新。

例如:

  1. 用户居住在cityA
  2. 用户从城市A到城市B
  3. 应该触发一个函数。

我看到 Google play 服务只提供了以下功能:

  1. public abstract Location getLastLocation (GoogleApiClient client)

  2. public abstract PendingResult<Status> requestLocationUpdates (GoogleApiClient client, LocationRequest request, LocationListener listener)

问题是:

我需要不断跟踪用户所在城市的位置变化,而不仅仅是在用户使用应用时。

我怎样才能做到这一点又不会太快耗尽电池?

【问题讨论】:

标签: android location battery android-gps


【解决方案1】:

您可以为此使用广播接收器。在https://developer.android.com/reference/android/content/BroadcastReceiver.html 有一个文档。

private static final LocationRequest REQUEST = LocationRequest.create() .setFastestInterval(60000) // in milliseconds .setInterval(180000) // in milliseconds .setPriority(LocationRequest.PRIORITY_LOW_POWER);

'@Override
public void onConnected(Bundle bundle) {
    LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient,
        REQUEST,
        this);  // LocationListener
}`

使用 FusedLocationApi,一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多