根据Android文档:
Google位置服务API是Google Play服务的一部分,它提供了一个功能更强大的高级框架,可自动处理位置提供程序,用户移动和位置准确性,而不是
android.location
中的平台位置API。
但是使用融合位置提供程序(来自Google Play服务中的位置API)我不知道如何检查用户是否启用了位置或禁用了位置。
使用旧的android.location
很容易:
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
但我不想同时使用Google Play服务融合位置提供商和旧的Android位置。
如何检查用户是否使用融合位置提供程序启用了该位置?
提前致谢。
请参阅SettingsApi:检查您的位置请求,然后确保为应用程序的位置需求正确配置了设备的系统设置。
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.
干杯!
如同现在在2019年
最新,最好和最短的方式是
public void booleanisLocationEnabled()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is new method provided in API 28
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isLocationEnabled();
} else {
// This is Deprecated in API 28
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
return (mode != Settings.Secure.LOCATION_MODE_OFF);
}
}
您也可以这样检查:
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean isEnabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);