【问题标题】:GoogleServicesAPI - the app doesn't check if GPS is enabledGoogleServicesAPI - 应用不检查 GPS 是否启用
【发布时间】:2016-07-22 21:39:26
【问题描述】:

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

我已经让我的应用程序连接到互联网,但是当我意识到手机无法检查 GPS 是否打开时,我遇到了一个问题。

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

我研究并遇到过onProviderEnabled,但我只看到它适用于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);
    }
}

【问题讨论】:

    标签: android gps


    【解决方案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 是您用于获取提供程序的请求

    【讨论】:

    • 所以我只是在 onConnected 中调用它吗?
    • 在 onCreate 中构建 locationsettings 并在 onConnected 中调用 checkLocationSettings
    • 在调用 onConnected 签入之前,请确保在 onCreate 中构建 LocationSettingsRequest、实际 LocationRequest 和 GoogleApiClient
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多