【问题标题】:GoogleAPIClient not connecting properly?GoogleAPIClient 连接不正确?
【发布时间】:2015-06-02 15:58:07
【问题描述】:

我正在尝试连接 mGoogleAPIClient,关注 this Guide。 当我调试时,会弹出一个小窗口,询问我想以哪个 googleplus 帐户登录。一旦我选择了我的主帐户并单击“确定”,它就会从那里停止,并且什么也不做。没有错误消息、警告、信息,什么都没有。

我的来源: 公共类 MainMenu_Activity 扩展 Activity 实现 GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener {

    private Button btnStart;
    private Button btnSubmit;
    private Button btnHOF;
    private GoogleApiClient mGoogleApiClient;
    public static int REQUEST_LEADERBOARD = 100;
    // Request code to use when launching the resolution activity
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;
    private static final String STATE_RESOLVING_ERROR = "resolving_error";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainmenu);

        mResolvingError = savedInstanceState != null
                && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);

        this.mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mResolvingError) {  // more about this later
            mGoogleApiClient.connect();
        }
    }

    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // Connected to Google Play services!
        // The good stuff goes here.
        System.out.println("CONNECTED!");
    }

    @Override
    public void onConnectionSuspended(int cause) {
        // The connection has been interrupted.
        // Disable any UI components that depend on Google APIs
        // until onConnected() is called.
        System.out.println("SUSPENDED!");
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        System.out.println("Connection Failed - mResolvingError=" + mResolvingError);
        if (mResolvingError) {
            System.out.println("Connection Failed - Already attempting to resolve...");
            // Already attempting to resolve an error.
            return;
        } else if (result.hasResolution()) {
            System.out.println("Connection Failed - resolving now...");
            try {
                mResolvingError = true;
                result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
            } catch (IntentSender.SendIntentException e) {
                System.out.println("Connection Failed - Exception...");
                // There was an error with the resolution intent. Try again.
                mGoogleApiClient.connect();
            }
        } else {
            // Show dialog using GooglePlayServicesUtil.getErrorDialog()
            Dialog errDia = GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 1);
            errDia.show();
            mResolvingError = true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_RESOLVE_ERROR) {
            mResolvingError = false;
            if (resultCode == RESULT_OK) {
                // Make sure the app is not already connected or attempting to connect
                if (!mGoogleApiClient.isConnecting() &&
                        !mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.connect();
                }
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError);
    }
  }

如您所见,我标记了某些地方以遵循该程序。 这就是 logcat 记录的内容:

06-02 17:52:48.175 2302-2302/com.dudewithfacial.game.gamebase I/System.out: 连接失败 - mResolvingError=false 06-02 17:52:48.175
2302-2302/com.dudewithfacial.game.gamebase I/System.out:连接失败 - 现在解决...

我按照指南一步一步来:( 好吧,显然不是,我在做什么错 Stackoverflow-Gurus? 谢谢男孩,真的很感激!

【问题讨论】:

  • 更新:出现此错误:06-02 18:42:20.656 2391-7463/? W/ActivityManager: mDVFSHelper.acquire() 06-02 18:42:20.656 2391-7463/? W/ActivityManager:活动作为新任务启动,因此取消活动结果。

标签: java android google-api connection google-api-client


【解决方案1】:

NVM,发现我不能使用 Launchmode、SingleUser、SingleInstance。 也没有配置一些请求的API,例如“.addApi(Plus.API)”

问题已解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多