3

我在理解PlusClient.Builder.clearScope().build()PlusClient.Builder.setScope( ).build() 有何不同时遇到了问题。在第一种情况下,我的加号按钮工作正常,但在后一种情况下,按钮呈灰色显示,没有出现任何 G+ 计数。

为什么调用setScope()会导致这个问题?

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

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.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.PlusOneButton;

public class SignInActivity extends ActionBarActivity 
  implements View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener
{
 private final String TAG = SignInActivity.class.getSimpleName();
 private final int REQUEST_CODE_RESOLVE_ERR = 9000;
 private final int PLUS_ONE_REQUEST_CODE = 1;
 private PlusOneButton mPlusOneBtn;
 private ProgressDialog mConnectionProgressDialog;
 private PlusClient mPlusClient;
 private Resources mRes; private String mURL;
 private ConnectionResult mConnectionResult;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_in_activity);
    mRes = getResources();
    mURL = mRes.getString(R.string.share_url);
    findViewById(R.id.sign_in_button_dude).setOnClickListener(this);
    mPlusOneBtn = (PlusOneButton) findViewById(R.id.plus_one_standard_button);
    mPlusClient = new PlusClient.Builder(this, this, this).clearScopes().build();
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
 }

 @Override
 protected void onStart() {
     super.onStart();
     mPlusClient.connect();
 }

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

 @Override
 protected void onResume() {
     super.onResume();
     mPlusOneBtn.initialize(mPlusClient, mURL, PLUS_ONE_REQUEST_CODE);
 }
}

工作加一键

但是现在当我使用 .setScopes() 时,标准的加一按钮是灰色的。

    mPlusClient = new PlusClient.Builder(this, this, this).
            .setScopes(Scopes.APP_STATE).setScopes(Scopes.GAMES)
            .setScopes(Scopes.PLUS_LOGIN).setScopes(Scopes.PLUS_PROFILE)
            .build();

变灰加一键

res/layout/sign_in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/sign_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" >

        <com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button_dude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/sign_out_button"
            style="@android:style/TextAppearance.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/sign_in_button_dude"
            android:text="@string/sign_out_button_name"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/revoke_access_button"
            style="@android:style/TextAppearance.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/sign_out_button"
            android:text="@string/revoke_access_button_name"
            android:textColor="@color/black" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sign_row"
        android:layout_marginBottom="8dp" >

        <com.google.android.gms.plus.PlusOneButton
            xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
            android:id="@+id/plus_one_standard_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            plus:size="standard" />

        <TextView
            android:id="@+id/sign_in_status"
            android:layout_below="@+id/plus_one_standard_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Default status" />
    </RelativeLayout>

</RelativeLayout>
4

1 回答 1

1

尝试在 onConnected 回调中初始化 mPlusOneBtn:

public void onConnected(Bundle connectionHint) {
    mPlusOneBtn.initialize(mPlusClient, mURL, PLUS_ONE_REQUEST_CODE);
}

来自http://developer.android.com/reference/com/google/android/gms/plus/PlusClient.html的文档:

“在调用异步 connect() 方法并调用侦听器的 onConnected(android.os.Bundle) 方法后使用 PlusClient。”

如果这不起作用,请确保正确初始化 PlusClient,检查 onConnectionFailed 返回的内容

public void onConnectionFailed(ConnectionResult result) {
    //check what result says
}
于 2013-08-20T04:38:10.603 回答