gpt4 book ai didi

Android 新的 LocationSettings 和 Geofencing

转载 作者:行者123 更新时间:2023-11-30 02:12:11 27 4
gpt4 key购买 nike

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;
}
}
}

最佳答案

I had the same problem and this is what I did

类定义:

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:

Used chaining to 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;
}
}
});
}

关于Android 新的 LocationSettings 和 Geofencing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com