在我的應用程序中,如果用戶沒有打開位置,我正在提示一個對話框,然後嘗試通過重寫活動結果返回該結果(可能不正確)。開始解析結果片段
這是一個片段內,從而不知道如何改變的事情:
這是我如何調用它與startResolutionForResult對話框:
public void checkDeviceLocationIsOn()
{
System.out.println("Test running setting request");
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied.
System.out.println("Test setting all fine starting location request");
getLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(getActivity(), LOCATION_SETTINGS_REQUEST_CODE);
System.out.println("Test setting not met starting dialog to prompt user");
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
然後我試圖得到這樣的結果下方(這不會被調用):提前
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case LOCATION_SETTINGS_REQUEST_CODE:
switch (resultCode) {
case Activity.RESULT_OK:
System.out.println("test user has turned the gps back on");
getLocation();
break;
case Activity.RESULT_CANCELED:
System.out.println("test user has denied the gps to be turned on");
Toast.makeText(getActivity(), "Location is required to order stations", Toast.LENGTH_SHORT).show();
break;
}
break;
}
}
感謝您的幫助
爲什麼使用'System.out.println()'而不是Android' Log'類? https://developer.android.com/reference/android/util/Log.html – AdamMc331
因爲它是爲我自己的測試目的 –
你有沒有得到任何解決方案,我面臨同樣的問題:( – r4jiv007