首页 文章

Android:Google登录GoogleApiClient.connect()

提问于
浏览
0

以下代码是从www.developers.google.com复制的

我想实现Google登录功能,但无法执行此操作 . 当我点击SignIn Button它要求许可并进行一些搜索然后没有任何反应,如果我调用GoogleApiClient.isConnected()它返回false .

我已导入BaseGameUtils并添加了line:compile project(':BaseGameUtils')

app_id是使用sha1生成的:keytool -exportcert -alias androiddebugkey -keystore C:\ Users \ usman \ .android \ debug.keystore -list -v

public class MenuActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
    boolean mExplicitSignOut = false;
    boolean mInSignInFlow = false; // set to true when you're in the middle of the
    // sign in flow, to know you should not attempt
    // to connect in onStart()
    GoogleApiClient mGoogleApiClient;  // initialized in onCreate
    private static int RC_SIGN_IN = 9001;
    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInFlow = true;
    private boolean mSignInClicked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);

        // Create the Google Api Client with access to the Play Game and Drive services.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER) // Drive API
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mInSignInFlow && !mExplicitSignOut) {
            // auto sign in
            //mGoogleApiClient.connect();
        }
    }



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

    @Override
    public void onConnected(Bundle connectionHint) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
    }



    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mResolvingConnectionFailure) {
            // Already resolving
            return;
        }

        if (mSignInClicked || mAutoStartSignInFlow) {
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = true;

            if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult, RC_SIGN_IN, "signin_other_error")) //R.string.signin_other_error
            {
                mResolvingConnectionFailure = false;
            }
        }

        // Put code here to display the sign-in button
    }

    @Override
    public void onConnectionSuspended(int i) {
        // Attempt to reconnect
        mGoogleApiClient.connect();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            mSignInClicked = false;
            mResolvingConnectionFailure = false;
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            } else {
                BaseGameUtils.showActivityResultError(this, requestCode, resultCode, 404); //R.string.signin_failure
            }
        }
    }

    // Call when the sign-in button is clicked
    private void signInClicked() {
        mSignInClicked = true;
        mGoogleApiClient.connect();
    }

    // Call when the sign-out button is clicked
    private void signOutclicked() {
        mSignInClicked = false;
        Games.signOut(mGoogleApiClient);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.sign_out_button) {
            // user explicitly signed out, so turn off auto sign in
            mExplicitSignOut = true;
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                Games.signOut(mGoogleApiClient);
                mGoogleApiClient.disconnect();
            }
        }

        if (view.getId() == R.id.sign_in_button) {
            // start the asynchronous sign in flow
            mSignInClicked = true;
            System.out.println("Calling Method: mGoogleApiClient.connect( )\n");
            mGoogleApiClient.connect();
        }
        else if (view.getId() == R.id.sign_out_button) {
            mSignInClicked = false;
            Games.signOut(mGoogleApiClient);

            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_button).setVisibility(View.GONE);
        }
    }
}

表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gsignin">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application android:allowBackup="true" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme">

    <meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <activity android:name="com.example.gsignin.Splash" android:label="@string/app_name" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name="com.example.gsignin.MenuActivity" android:label="@string/app_name" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="com.example.gsignin.MenuActivity"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

    </application>
</manifest>

1 回答

  • 0

    对于Google登录,您可能需要尝试从Android Sign-in Quickstart开始,然后从那里进行集成 . 如果您正在使用Play游戏服务,BaseGamesUtils可能会有所帮助,但不是必需的 .

    接下来,除非您要更新/迁移旧版Play游戏服务项目,否则请从清单和应用程序中删除对 APP_STATE 的所有引用 . 用于Play游戏服务的App State API已被弃用,可能会导致授权问题 .

    您表明您的 APP_ID 来自 keytool -list 来电,但这听起来不正确 . APP_ID 对应于Play游戏服务应用程序ID,仅用于Play游戏服务功能 . 你应该只运行:

    keytool -list -keystore ~/.android/debug.keystore
    

    获取SHA-1哈希值 . 此哈希用于在Google Developer Console上注册您的Android应用程序(包名称为 com.example.gsignin ) .

    希望有所帮助,你可能最好从我指向你的工作样本开始,然后在那里工作,找出事情的发展方向 .

相关问题