GoogleServicesAPI - 该应用不会检查GPS是否已启用

时间:2016-07-22 13:31:24

标签: android gps

我正在尝试让我的应用程序通过GooglePlayServices访问地点。

我的应用程序已连接到互联网,但当我意识到手机没有检查GPS是否打开时,我已经搁浅了。

如果GPS已开启,手机会自动开始更新。但是,当GPS关闭时,手机根本没有反应。

我已经研究过并且遇到过ProViderEnabled,但我只看到它适用于LocationManager,并且我不确定它是否也适用于GooglePlayAPI。

如何让我的应用程序检查GPS是否打开?

onConnected():

public void onConnected(Bundle connectionHint) {
    Toast.makeText(this, "You have connected", Toast.LENGTH_LONG).show();

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);

        Location NewLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);

        //Getting initial location and time
        PhoneBelt.setNewLocation(NewLocation);
        long NewLocationTime = new Date().getTime();
        PhoneBelt.setNewLocationTime(NewLocationTime);
    }
}

1 个答案:

答案 0 :(得分:1)

使用以下代码,它使用googleSettingsAPI检查位置是否已启用。如果没有,它将显示类似于谷歌地图的对话框:

    protected void buildLocationSettingsRequest() {
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    mLocationSettingsRequest = builder.build();
    }


    protected void checkLocationSettings() {
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest
            );
    result.setResultCallback(this);
    }


@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
    final Status status = locationSettingsResult.getStatus();

    switch (status.getStatusCode()) {


        case LocationSettingsStatusCodes.SUCCESS:
            Log.d("FragmentCreate", "All location settings are satisfied.");

            pd.show();


            try {

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


            }catch (SecurityException se){
                Log.d("FragmentCreate","You don't have permissions");
                pd.dismiss();
                errortext.setVisibility(View.VISIBLE);
                errortext.setText("Please provide Location permission to continue, Settings->Apps->RecommendedApp->Permissions");
                Toast.makeText(this,"Please provide location permissions to continue",Toast.LENGTH_SHORT).show();
            }


            break;


        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            Log.d("FragmentCreate", "Location settings are not satisfied. Show the user a dialog to" +
                    " upgrade location settings ");

            try {
                // Show the dialog by calling startResolutionForResult(), and check the result
                // in onActivityResult().
                status.startResolutionForResult(TabbedResult.this, REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                Log.i("FragmentCreate", "PendingIntent unable to execute request.");
            }
            break;



        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            Log.d("FragmentCreate", "Location settings are inadequate, and cannot be fixed here. Dialog " +
                    "not created.");
            break;
    }
}

mGoogleApiClient是您构建的googleApiClient对象,而LocationSettingsRequest是您用于获取提供商的请求