How to stop properly FusedLocationProviderClient?

时间:2017-12-18 08:19:51

标签: java android fusedlocationproviderclient

I use top within a FusedLocationProviderClient.
I would like to "stop" it in a right way.

Is it good to use following code?

Service

And the rest code

 @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop Looper of FusedLocationProviderClient  
        if (locationClient != null) {
            locationClient = null;
        }
    }

2 个答案:

答案 0 :(得分:2)

只需在onDestroy

中调用removeLocationUpdates即可

对于requestLocationUpdates,它说:

  

此调用将保持Google Play服务连接处于活动状态,因此请确保在不再需要时调用removeLocationUpdates(LocationCallback),否则您将失去自动连接管理的优势。

答案 1 :(得分:0)

只需将onDestroy();更改为:

@Override
    protected void onDestroy() {
        super.onDestroy();
        if(locationclient!=null)
            locationclient.disconnect();
    }