从不调用来自ResultCallback <locationsettingsresult>的onResult()回调

时间:2016-10-06 08:56:26

标签: java android callback google-play-services android-location

我正在尝试检索用户的位置设置,然后检索他们的位置,但我无法启动onResult回调。我首先在GoogleClientApi中初始化了onCreate()对象并尝试了调试,它在result.setResultCallback()断点处显示“没有这样的实例字段”。我花了很多时间在这上面,请帮帮我!

public class MainActivity extendsAppCompatActivity
implements,OnConnectionFailedListener{

private static final int PERMISSION_CODE = 23;
private static final int RESOLUTION_CODE = 0x1;
public GoogleApiClient googleClient;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    checkLocationSettings();

}

public void checkLocationSettings() {

     googleClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .addOnConnectionFailedListener(this)
            .build();

    LocationSettingsRequest.Builder locationSettingsBuilder = new LocationSettingsRequest.Builder()
            .addLocationRequest(new LocationRequest().setInterval(5000)).setAlwaysShow(true);

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleClient
            , locationSettingsBuilder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    checkLocationPermission();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try {
                        status.startResolutionForResult(MainActivity.this, RESOLUTION_CODE);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Toast.makeText(MainActivity.this, "Location is missing", Toast.LENGTH_LONG).show();
            }

        }
    });

}

private void checkLocationPermission() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_CODE);
    } else {
        googleClient.connect();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case RESOLUTION_CODE:
            switch (resultCode) {
                case RESULT_OK:
                    checkLocationPermission();
                    break;
                case RESULT_CANCELED:
                    Toast.makeText(MainActivity.this, "Location is required, turn it on", Toast.LENGTH_SHORT).show();
                    break;
            }
            break;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                googleClient.connect();
            } else {
                Toast.makeText(MainActivity.this, "Error Activty", Toast.LENGTH_LONG).show();
            }
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleClient);
    textView.setText(location.getLatitude() + location.getLongitude() + "");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
protected void onStop() {
    super.onStop();
    if(googleClient.isConnected()){
        googleClient.disconnect();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从这些SO主题No such instance fieldInitialize boolean value "no such instance field"开始,如果代码没有问题,请尝试重新启动Android Studio。也许它使用的是先前版本中的错误文件。此帖子:Why is LocationSettingsResult startResolutionForResult not calling onActivityResult?也可能有助于如何连接到GoogleApiClient以获取用户的位置。