无法实施Google Play使用Android Studio中的应用登录

时间:2015-08-06 22:26:15

标签: android android-studio google-play-services google-signin

我已按照谷歌开发者网站上的说明进行操作,但无法正确使用...

以下是我的MainActivity的相关代码段:

进口:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;
import com.google.example.games.basegameutils.BaseGameUtils;

实现:

public class MainActivity extends Activity implements View.OnTouchListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{ ...}

变量:

    GoogleApiClient mGoogleApiClient;

private static int RC_SIGN_IN = 9001;

private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;



SharedPreferences stats;
SharedPreferences achievements;
SharedPreferences.Editor stat_editor;
SharedPreferences.Editor achievement_editor;

这是实例化的onCreate()

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    .addScope(new Scope(Scopes.PROFILE))
                    // add other APIs and scopes here as needed
            .addApi(Plus.API)
            .build();

活动周期方法:

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

实施方法:

    @Override
public void onConnected(Bundle bundle) {
    //hide sign in  button
}

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


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

    // if the sign-in button was clicked or if auto sign-in is enabled,
    // launch the sign-in flow
    if (mSignInClicked || mAutoStartSignInflow) {
        mAutoStartSignInflow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;

        // Attempt to resolve the connection failure using BaseGameUtils.
        // The R.string.signin_other_error value should reference a generic
        // error string in your strings.xml file, such as "There was
        // an issue with sign-in, please try again later."
        if (!BaseGameUtils.resolveConnectionFailure(this,
                mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }

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

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 {
            // Bring up an error dialog to alert the user that sign-in
            // failed. The R.string.signin_failure should reference an error
            // string in your strings.xml file that tells the user they
            // could not be signed in, such as "Unable to sign in."
            BaseGameUtils.showActivityResultError(this,
                    requestCode, resultCode, R.string.sign_in_failed);
        }
    }
}

注意:我目前没有登录按钮,只是尝试让用户自动提示登录。

应用程序弹出后会崩溃一秒钟。

我认为可能缺少元标记或权限?目前这就是我的全部。

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

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

另一种可能性是谷歌开发者控制台上的设置不正确。我需要做些什么才能让这个东西连接起来?!我搞不清楚了!!感谢。

08-06 17:17:23.910  30903-30939/? E/HTTPMetricsTransport﹕ transmit - MissingCredentialsException while transmitting;
amazon.communication.MissingCredentialsException: Static Credential is unavailable.
        at com.amazon.client.metrics.transport.StaticCredentialRequestSigner.signRequest(StaticCredentialRequestSigner.java:44)
        at com.amazon.client.metrics.transport.MetricsHttpRequestSigner.signRequest(MetricsHttpRequestSigner.java:54)
        at amazon.communication.srr.HttpClientSrrManager.makeRequestSync(HttpClientSrrManager.java:190)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.makeRequest(HTTPMetricsTransport.java:286)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.attemptToTransmit(HTTPMetricsTransport.java:230)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.attemptToTransmit(HTTPMetricsTransport.java:235)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.transmit(HTTPMetricsTransport.java:202)
        at com.amazon.client.metrics.batch.transmitter.BatchTransmitter$QueuePusher.sendBatches(BatchTransmitter.java:161)
        at com.amazon.client.metrics.batch.transmitter.BatchTransmitter$QueuePusher.run(BatchTransmitter.java:127)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

另外,我读了一些关于生成客户端ID并下载JSON文件并将其导入项目的内容,这可能是问题所在?我生成了一个客户端ID,但没有包含任何JSON文件,我该怎么做?

更新

我改为:

            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
                    // add other APIs and scopes here as needed
            .addApi(Plus.API)
            .build();

删除我原来拥有的这两行:

                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    .addScope(new Scope(Scopes.PROFILE))

应用程序在加载时不再崩溃,但仍然没有尝试连接,或者没有谷歌登录窗口弹出。

1 个答案:

答案 0 :(得分:2)

我看到你不再有你抱怨的错误,唯一缺少的就是连接的步骤。尝试添加:

if (mGoogleApiClient.isConnected()) {
    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
    mGoogleApiClient.disconnect();
    mGoogleApiClient.connect();
} else if (!mGoogleApiClient.isConnecting()) {
    mSignInClicked = true;
    // uncomment the next line if you have this method.
    // resolveSignInError();
}

onResume

- 编辑 -

我看到您收到错误API_UNAVAILABLE。我认为您尚未激活Google+ API。转到the official Google tutorial, Step 1。请密切关注第4步,并确保启用Google+ API。你需要看到这张照片中的内容。

enter image description here

注意&#34;禁用API&#34;按钮。这意味着API肯定会启用。

这可能是你问题的关键还有一件事。尝试将此权限添加到清单

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