gpt4 book ai didi

Android:使用 Fused Location Provider 检查是否启用了位置服务

转载 作者:IT王子 更新时间:2023-10-28 23:33:45 27 4
gpt4 key购买 nike

根据 Android 文档:

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy than the platform location API in android.location.

但使用融合位置提供程序(来自 Google Play 服务中的位置 API)我不知道如何检查用户是否启用或禁用了位置。

使用 old android.location 很容易:

locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

但我不想同时使用 Google Play Services 融合位置提供程序和 android 位置。

如何检查用户是否使用 Fused Location Provider 启用了位置?

提前致谢。

最佳答案

This android developer training tutorial could help - 这是基础知识:

在 Activity onCreate() 中运行的代码:

 // Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

mGoogleApiClient.connect();

LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);

PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {

final Status status = locationSettingsResult.getStatus();
final LocationSettingsStates LS_state = locationSettingsResult.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.

break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(LocationByGPS.this, REQUEST_CHECK_SETTINGS);

} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.

break;
}
}
});

覆盖此方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
GetUserLocation();//FINALLY YOUR OWN METHOD TO GET YOUR USER LOCATION HERE
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to

break;
default:
break;
}
break;
}
}

记得在你的类里面实现这些:

public class MyClass extends AppCompatActivity implements
ActivityCompat.OnRequestPermissionsResultCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{

protected static final int REQUEST_CHECK_SETTINGS = 0x1;


/*
your code i.e. with the above code etc....
*/

}

Good explanation here in this Google developer link.

干杯!

关于Android:使用 Fused Location Provider 检查是否启用了位置服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com