【问题标题】:How to stop requestLocationUpdates() after a certain amount of time?如何在一定时间后停止 requestLocationUpdates()?
【发布时间】:2015-07-17 09:30:56
【问题描述】:

在我的 Android 应用程序中,我使用 GoogleApiClient 来处理位置服务。当我通过以下方式调用requestLocationUpdates() 时,一切正常:

locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment);

mGoogleApiClient 在我的活动的onCreate() 方法中初始化:

private GoogleApiClient mGoogleApiClient;
[...]

// onCreate() method in my activity
mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Plus.API, Plus.PlusOptions.builder().build())
    .addApi(LocationServices.API)
    .addScope(new Scope("email"))
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

而locationProvider 和locationRequest 在我的片段的onAttach() 方法中初始化,该片段也实现了com.google.android.gms.location.LocationListener:

//onAttach() method in my fragment
this.locationProvider = LocationServices.FusedLocationApi;

this.locationRequest = new LocationRequest();
this.locationRequest
    .setInterval(Constants.GOOGLE_LOCATION_INTERVAL)
    .setFastestInterval(Constants.GOOGLE_FASTEST_LOCATION_INTERVAL)
    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

问题是,有时用户可以要求在室内环境中检索她的位置,所以我想在一定时间后终止requestLocationUpdates() 请求。

到目前为止,我已经尝试了以下解决方案,但都没有成功:

1) 使用 Looper 和 Handler。实际上这个解决方案与旧的LocationManager 配合得很好。

Looper looper = Looper.myLooper();
Handler handler = new Handler(looper);

handler.postDelayed(new Runnable() {
@Override
    public void run() {
        locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment, looper);
    }
}, Constants.LOCATION_TIMEOUT_MS);

2) 仅使用处理程序

Handler handler = new Handler();

handler.postDelayed(new Runnable() {
@Override
    public void run() {
        locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment);
    }
}, Constants.LOCATION_TIMEOUT_MS);

但是,在这两种情况下,超时(由Constants.LOCATION_TIMEOUT_MS 表示)永远不会过期。 GPS 服务一直在不停地工作。

【问题讨论】:

    标签: android gps google-api location handler


    【解决方案1】:

    首先这两个代码是相同的:

    Looper looper = Looper.myLooper();
    Handler handler = new Handler(looper);
    

    和

    Handler handler = new Handler();
    

    如果您查看源代码,您可以看到 Handler 的空构造函数内部使用了Looper.myLooper();

    我不确定您认为这可能会在哪里取消请求,时间过去后,您再次致电requestLocationUpdates。所以是的,它会继续尝试获取 GPS 锁定。

    我会建议你两种你应该怎么做的方法。

    1. 第一个更容易,但我不确定它的效果如何,因为我只将它用于被动位置更新。在您的请求中使用expiration,在过期时间之后,位置服务应该会自动退出。

      locationRequest.setExpirationDuration(Constants.LOCATION_TIMEOUT_MS);
      
    2. 第二个是使用正确的方法,即removeLocationUpdates

      locationProvider.removeLocationUpdates(
                   mGoogleApiClient, 
                   (LocationListener) thisFragment);
      

    【讨论】:

    • 天啊!我需要一个假期......你绝对是对的!我在处理程序中调用requestLocationUpdates 而不是removeLocationUpdates...感谢您提供有关处理程序构造函数的信息。
    • 哈哈。我们都在那个需要假期但没有假期的地方。也可以试试setExpirationDuration(可能是一个更好的方法),不要忘记在片段onStop时removeLocationUpdates,否则你可能会泄漏一些内存。
    • @Budius 你有这个声明的来源吗? “[...] 在过期时间之后,定位服务应该会自动退出”。我一直试图弄清楚到期时会发生什么,但还没有看到任何确定的东西。谢谢!
    • @stkent 方法的名字是setExpirationDuration,所以过了duration,就过期了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2019-04-20
    • 2013-09-20
    相关资源
    最近更新 更多