LocationRequest.PRIORITY_HIGH_ACCURACY在API 21中不起作用

时间:2018-07-30 06:03:23

标签: android location google-play-services

我正在尝试启用位置系统设置并将其设置为HIGH ACCURACY。 当我按OK时,该代码在21以上的API上运行良好,位置设置已打开并设置为HIGH ACCURACY,但对于21及以下的API,该位置已打开但设置为省电。

public class MainActivity extends AppCompatActivity {

    LocationRequest mLocationRequest;

    private static final int REQUEST_CHECK_SETTINGS = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("LogCheck", "createLocationRequest");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(2000);
        mLocationRequest.setFastestInterval(2000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
        SettingsClient client = LocationServices.getSettingsClient(this);
        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
        task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            }
        });

        task.addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(MainActivity.this,
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                }
            }
        });

    }

API 25 request    API 25请求
API 25 setting    API 25设置 API 21 request    API 21请求 API 21 setting    API 21设置

您可以在我请求的代码中看到

 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

但是它似乎在API 21中不起作用。

0 个答案:

没有答案