Android新的LocationSettings和Geofencing

时间:2015-04-28 23:12:57

标签: android google-play-services android-location android-geofence

我试图这样做7天以来没有结果。问题是ResultCallback,对于地理围栏我需要ResultCallback而对于LocationSettings我需要ResultCallback,有没有办法做到这一点? 我很感激一些提示...... 我正在使用最新的Google Play Services 7.0 感谢您的支持:)

public class GeofenceIntentService 
       extends IntentService 
       implements GoogleApiClient.ConnectionCallbacks,
                  GoogleApiClient.OnConnectionFailedListener, 
                  ResultCallback<Status>, ResultCallback<LocationSettingsResult> {

    // onResult for geofencing
    public void onResult(Status status) {
        Log.i(TAG, "onResult");
        if (status.isSuccess()) {
            Log.i(TAG, "status.isSuccess()");
        } else {
          String errorMessage = GeofenceErrorMessages.getErrorString(getApplicationContext(),
                status.getStatusCode());
          Log.e(TAG, errorMessage);
        }
    }

    // onResult for LocationSettings
    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "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(BaseActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        Log.i(TAG, "User agreed to make required location settings changes.");
                        startLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i(TAG, "User chose not to make required location settings changes.");
                        break;
                }
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

我有同样的问题,这就是我做的事情

课程定义:

public class Activity/Service extends AppCompatActivity/IntentService implements
        ConnectionCallbacks, 
        OnConnectionFailedListener,
        ResultCallback<Status> {
   ...
}

对于GeofencingApi:

public void addGeofencesButtonHandler(View view) {
    if (!mGoogleApiClient.isConnected()) {
        // Log.e(TAG, "Not connected");
        return;
    }

    try {
        PendingResult<Status> result =
                LocationServices.GeofencingApi.addGeofences(
                        mGoogleApiClient,
                        getGeofencingRequest(),
                        getGeofencePendingIntent()
                );

        result.setResultCallback(this); 
    } catch (SecurityException securityException) {
        logSecurityException(securityException);
    }
}

对于SettingsApi:

  

使用链接到setResultCallback

protected void checkLocationSettings() {
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest
            ).setResultCallback(new ResultCallback<LocationSettingsResult>() {
                @Override
                public void onResult(LocationSettingsResult locationSettingsResult) {
                    final Status status = locationSettingsResult.getStatus();
                    switch (status.getStatusCode()) {
                        case LocationSettingsStatusCodes.SUCCESS:
                            Log.i(TAG, "All location settings are satisfied.");
                            if (!mRequestingLocationUpdates) {
                                mRequestingLocationUpdates = true;
                                setButtonsEnabledState();
                                startLocationUpdates();
                            }
                            break;
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                                    "upgrade location settings ");
                            try {
                                status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                            } catch (IntentSender.SendIntentException e) {
                                Log.i(TAG, "PendingIntent unable to execute request.");
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                                    "not created.");
                            break;
                    }
                }
            });
}