gog*_*est 5 android google-plus google-play-services
我是Android开发的新手,希望能为我面临的问题提供一些建议.
我的应用要求我实施Google +登录按钮.
我的进步
我的问题
这可以防止用户选择其他登录选项.
我想知道我的代码有什么问题,任何帮助将不胜感激.谢谢.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// facebook sign in
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_sign_in);
facebookLoginSetup(findViewById(android.R.id.content).getRootView());
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
SignInButton sign_in_button = (SignInButton) findViewById(R.id.sign_in_button);
setGooglePlusButtonText(sign_in_button, getString(R.string.google_login_button_label));
findViewById(R.id.sign_in_button).setOnClickListener(this);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.global_message_loading));
mProgressDialog.setCancelable(false);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
// google
if (requestCode == RC_SIGN_IN) {
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.reconnect();
}
} else {
// facebook
// call registered call back method
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onConnectionSuspended(int cause)
{
mGoogleApiClient.connect();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.sign_in_button && !mGoogleApiClient.isConnecting()) {
if(!mProgressDialog.isShowing()){
mProgressDialog.show();
}
mSignInClicked = true;
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
mSignInClicked = false;
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
String userEmail = Plus.AccountApi.getAccountName(mGoogleApiClient);
createUser(userEmail);
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()) {
try {
Log.d(MainActivity.TAG, "onConnectionFailed keep retrying");
mIntentInProgress = true;
startIntentSenderForResult(result.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
}
}
}
// google custom button
protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) {
for (int i = 0; i < signInButton.getChildCount(); i++) {
View v = signInButton.getChildAt(i);
if (v instanceof TextView) {
TextView tv = (TextView) v;
tv.setTextSize(15);
tv.setTypeface(null, Typeface.NORMAL);
tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
tv.setText(buttonText);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在onActivityResult方法中检查 resultCode。
if(responseCode == RESULT_OK){
//User clicked sign in do your stuff
}else if(responseCode == RESULT_CANCELED){
//User clicked cancel
mIntentInProgress = true;
}
//if (!mIntentInProgress && result.hasResolution()) {
Run Code Online (Sandbox Code Playgroud)
现在onConnectionFailed不会执行startIntentSenderForResult