本文介绍了永远不会调用google api客户端回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Services API获取最后一个已知的位置,但是在我构建GoogleApiClient之后,从未触发过任何回调方法.

I am trying to get the last known location using google services API, but after I build the GoogleApiClient, no callback method is ever fired.

我的活动如下:

public class MainActivity extends Activity implements FragmentObserver, SessionSpotListObserver,
                                                        ConnectionCallbacks, OnConnectionFailedListener{

//Objects used for the location API
    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    // Request code to use when launching the resolution activity
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Unique tag for the error dialog fragment
    private static final String DIALOG_ERROR = "dialog_error";
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;
    public static final String STATE_RESOLVING_ERROR = "resolving_state";
    //Request code to use when launching the activity to fix the connection to google API
    private static final int REQUEST_SOLVE_CONNEXION = 999;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//We make sure that google play service is available on the device
         if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS){
            //We get a connection to the Google Play Service API to get the location of the user
            buildGoogleApiClient();
         }
         else {
             GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
                     this,
                     REQUEST_SOLVE_CONNEXION);
         }
}

@Override
    public void onConnectionFailed(ConnectionResult result) {
         if (mResolvingError) {
                // Already attempting to resolve an error.
                return;
            } else if (result.hasResolution()) {
                try {
                    mResolvingError = true;
                    result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
                } catch (SendIntentException e) {
                    // There was an error with the resolution intent. Try again.
                    mGoogleApiClient.connect();
                }
            } else {
                // Show dialog using GooglePlayServicesUtil.getErrorDialog()
                showErrorDialog(result.getErrorCode());
                mResolvingError = true;
            }
        }

        // The rest of this code is all about building the error dialog

        /* Creates a dialog for an error message */
        private void showErrorDialog(int errorCode) {
            // Create a fragment for the error dialog
            ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
            // Pass the error that should be displayed
            Bundle args = new Bundle();
            args.putInt(DIALOG_ERROR, errorCode);
            dialogFragment.setArguments(args);
            dialogFragment.show(getFragmentManager(), "errordialog");
        }

        /* Called from ErrorDialogFragment when the dialog is dismissed. */
        public void onDialogDismissed() {
            mResolvingError = false;
        }

        /* A fragment to display an error dialog */
        public static class ErrorDialogFragment extends DialogFragment {
            public ErrorDialogFragment() { }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Get the error code and retrieve the appropriate dialog
                int errorCode = this.getArguments().getInt(DIALOG_ERROR);
                return GooglePlayServicesUtil.getErrorDialog(errorCode,
                        this.getActivity(), REQUEST_RESOLVE_ERROR);
            }

            @Override
            public void onDismiss(DialogInterface dialog) {
                ((MainActivity)getActivity()).onDialogDismissed();
            }
    }

    @Override
    public void onConnected(Bundle arg0) {
         mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
         Log.d("API Connection", "The API has connected and the last location is :" + mLastLocation);
            if (mLastLocation != null) {

            }


    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

    /**
     * Creates the connexion to the Google API. Once the API is connected, the
     * onConnected method is called.
     */
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    }

我在所有回调方法上都设置了断点,这就是我知道没有调用断点的方式.

I placed breakpoints on all callback methods, that is how I know that none is called.

因为在此阶段我没有使用Google Map Api,所以我没有注册我的应用程序以获取密钥.即使我刚刚知道位置,也需要这样做吗?

Because at this stage I am not using Google Map Api, I did not register my app to get a key. Do I need to do that even if I just get the location ?

请随时告诉我是否需要更多信息.

Don't hesitate to tell me if you need more info.

谢谢大家.

推荐答案

在构建GoogleApiClient之后,您再也不会调用mGoogleApiClient.connect().您的onCreate()应该是:

You never call mGoogleApiClient.connect() after building your GoogleApiClient. Your onCreate() should instead be:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    buildGoogleApiClient();
    mGoogleApiClient.connect();
}

请注意,如果您使用的是GoogleApiClient,则无需调用GooglePlayServicesUtil.isGooglePlayServicesAvailable(),因为connect()也包括该检查.

Note that there is no need to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() if you are using GoogleApiClient as connect() includes that check as well.

这篇关于永远不会调用google api客户端回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 22:05