我在理解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>