使用Google Play服务Location API收听位置服务更新

时间:2018-08-30 16:10:03

标签: android location google-play-services

我正在尝试使用Google Play服务的位置API(com.google.android.gms.location)收听以更新位置服务(即用户在系统设置中打开/关闭位置) )。

我知道我可以投票此信息:

import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.SettingsClient;


LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
        .addLocationRequest(mLocationRequest);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();

// ...

SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

我也知道我可以使用 android SDK来收听

import android.location.LocationManager;
import android.provider.Settings

private BroadcastReceiver locationServicesBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
            int locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
            boolean isEnabled = locationMode != Settings.Secure.LOCATION_MODE_OFF;
        }
    }
};

但是我可以使用播放服务API吗?

1 个答案:

答案 0 :(得分:0)

Google Play服务较新,并且在管理此类请求方面提供了更好的功能;他们更实惠。它们处理内存,电池使用情况和连接的能力远远优于旧的android.location库。 Here it is where google deprecate it