android - Android 上的 Google+ 登录按钮在点击时没有任何反应

标签 android google-plus google-play-services

我正在尝试在我的 Android 应用程序中实现 Google+ 登录。 我遵循了 Google+ API 教程。但是,当我单击登录按钮时,没有任何反应,没有出现登录对话框或提示。 Logcat 也没有显示任何错误。当我遵循 Google+ API 文档时,我似乎无法理解为什么这不起作用。

这是我的代码:

package com.chromiumapps.fost;

import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static android.view.View.*;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;

public class FostActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, OnClickListener {

    /* Called when the activity is first created. */

    /*
     * public boolean Facebook = false; public boolean Twitter = false; public
     * boolean Googleplus = false; public boolean Blogger = false;
     */

    private MainFragment mainFragment;
    SharedPreferences pref;




    private static final int RC_SIGN_IN=0;
    private GoogleApiClient mGoogleApiClient;
    private boolean mIntentInProgress;
    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getPreferences(0);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString("CONSUMER_KEY", CONSUMER_KEY);
        edit.putString("CONSUMER_SECRET", CONSUMER_SECRET);
        edit.commit();

        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, mainFragment).commit();
            //s = new ShareBarActivity();
            //getSupportFragmentManager().beginTransaction()
                    //.add(android.R.id.content, s).commit();


        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
            //linkedInFragment = (LinkedIn_Fragment) getSupportFragmentManager().findFragmentById(android.R.id.content);

        }
        setContentView(R.layout.main);


        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(Plus.API, new Plus.PlusOptions.Builder().build())
                            .addScope(Plus.SCOPE_PLUS_LOGIN)
                            .build();


    }

    protected void onStart(){
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop(){
        super.onStop();
        if (mGoogleApiClient.isConnected()){
            mGoogleApiClient.disconnect();

        }

    }
    protected void onAcitivityResult(int requestCode, int ResponseCode, Intent intent){
        if (requestCode==RC_SIGN_IN){
            if (ResponseCode != RESULT_OK) {
                  mSignInClicked = false;
                }

                mIntentInProgress = false;

             if (!mGoogleApiClient.isConnecting()) {
                 mGoogleApiClient.connect();
              }
        }
    }



    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        mSignInClicked = false;

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub
        mGoogleApiClient.connect();
    }



    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        if (!mIntentInProgress) {
            // Store the ConnectionResult so that we can use it later when the user clicks
            // 'sign-in'.
            mConnectionResult = result;

            if (mSignInClicked) {
              // The user has already clicked 'sign-in' so we attempt to resolve all
              // errors until the user is signed in, or they cancel.
              resolveSignInError();
            }
          }

    }

    protected void resolveSignInError(){
        if(mConnectionResult.hasResolution()){
            try {
                  mIntentInProgress = true;
                  startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
                      RC_SIGN_IN, null, 0, 0, 0);
                } catch (SendIntentException e) {
                  // The intent was canceled before it was sent.  Return to the default
                  // state and attempt to connect to get an updated ConnectionResult.
                  mIntentInProgress = false;
                  mGoogleApiClient.connect();
                }
              } 

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.sign_in_button && !mGoogleApiClient.isConnecting()){
            mSignInClicked=true;

            resolveSignInError();

        }

        if(v.getId()==R.id.sign_out_button){
            if(mGoogleApiClient.isConnected()){
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();
            }
        }


    }
}

最佳答案

在 mGoogleApiClient 试试这个

   mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApiIfAvailable(Drive.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build())
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

关于android - Android 上的 Google+ 登录按钮在点击时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754889/

相关文章:

android - Android 客户端中经过身份验证的 Cloud Endpoint 间歇性故障

android - 这是 Android 4.0 中的新实现吗?

javascript - 未捕获( promise )错误 : "Device" plugin is not implemented on android

java - 在 AsyncTask 中打开失败 : EACCES (Permission denied) from IoBridge. java

ios - 如何在ios项目中实现Google Drive和Google Plus sdk

android - Google 询问 Google Play 是否需要政策,但我在 list 文件中拥有有效权限?

android - 为什么没有任何东西显示出此 flutter 支架的主体?

Node.js OAuth2 : Get Google+ activities

javascript - 使用 jQuery 获取 Google+ 订阅计数

android - 将Google Play服务从6.5.87更新到7.5.0时出错