我尝试在我的应用程序的注册活动中使用Google+实施登录。我按照谷歌教程,但当我点击登录按钮时,它显示选择帐户的对话框,选择帐户后,它什么都不做。我想我犯了一些错误, 这是代码:
public class SignupActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener, GoogleApiClient.ConnectionCallbacks {
private CallbackManager callbackManager;
GoogleApiClient google_api_client;
SignInButton signIn_btn;
private boolean is_signInBtn_clicked;
private boolean is_intent_inprogress;
private int request_code;
private static final int SIGN_IN_CODE = 0;
private ConnectionResult connection_result;
GoogleApiAvailability google_api_availability;
SharedPreferences sharedPref;
SharedPreferences.Editor loginStateEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
buidNewGoogleApiClient();
setContentView(R.layout.activity_signup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Sign Up");
setActionBar(toolbar);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
sharedPref= getSharedPreferences("myPref", Context.MODE_PRIVATE);
loginStateEditor=sharedPref.edit();
//Customize sign-in button.a red button may be displayed when Google+ scopes are requested
custimizeSignBtn();
setBtnClickListeners();
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Intent i = new Intent(getApplicationContext(), GetLocation.class);
loginStateEditor.putBoolean("success_login", true);
loginStateEditor.commit();
startActivity(i);
finish();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),
"Login attempt canceled.", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(),
"Login attempt failed", Toast.LENGTH_SHORT).show();
}
});
}
private void buidNewGoogleApiClient(){
google_api_client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
private void custimizeSignBtn(){
signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);
signIn_btn.setSize(SignInButton.SIZE_STANDARD);
signIn_btn.setScopes(new Scope[]{Plus.SCOPE_PLUS_LOGIN});
}
private void setBtnClickListeners(){
signIn_btn.setOnClickListener(this);
}
public void onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_signup, container, false);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
callbackManager.onActivityResult(requestCode, responseCode, intent);
if (requestCode == SIGN_IN_CODE) {
request_code = requestCode;
if (responseCode != RESULT_OK) {
is_signInBtn_clicked = false;
}
is_intent_inprogress = false;
if (!google_api_client.isConnecting()) {
google_api_client.connect();
}
}
}
protected void onStart() {
super.onStart();
//connect GoogleApiClient
google_api_client.connect();
}
protected void onStop() {
super.onStop();
//disconnect GoogleApiClient
if (google_api_client.isConnected()) {
google_api_client.disconnect();
}
}
protected void onResume(){
super.onResume();
if (google_api_client.isConnected()) {
google_api_client.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
is_signInBtn_clicked = false;
Intent i=new Intent(SignupActivity.this,GetLocation.class);
startActivity(i);
finish();
}
@Override
public void onConnectionSuspended(int i) {
google_api_client.connect();
}
@Override
public void onClick(View v) {
int id = v.getId();
if(id==R.id.sign_in_button)
Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();
gPlusSignIn();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (!connectionResult.hasResolution()) {
google_api_availability.getErrorDialog(this, connectionResult.getErrorCode(),request_code).show();
return;
}
if (!is_intent_inprogress) {
connection_result = connectionResult;
if (is_signInBtn_clicked) {
resolveSignInError();
}
}
}
private void gPlusSignIn() {
if (!google_api_client.isConnecting()) {
is_signInBtn_clicked = true;
resolveSignInError();
}
}
/*
Method to resolve any signin errors
*/
private void resolveSignInError() {
if (connection_result.hasResolution()) {
try {
is_intent_inprogress = true;
connection_result.startResolutionForResult(this, SIGN_IN_CODE);
} catch (IntentSender.SendIntentException e) {
is_intent_inprogress = false;
google_api_client.connect();
}
}
}
}