我正尝试这样做,因为7天没有结果。问题是ResultCallback,对于地理围栏,我需要ResultCallback,而对于LocationSettings,我需要ResultCallback,有没有办法做到这一点?
我感谢一些提示...
我正在使用最新的Google Play服务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;
}
}
}
最佳答案
我遇到了同样的问题,这就是我所做的
类定义:
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;
}
}
});
}