我是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;
}
}
}
答案 0 :(得分:0)
在 onActivityResult 方法中检查resultCode。
d_
现在 onConnectionFailed 不会执行 startIntentSenderForResult
答案 1 :(得分:0)
我也有同样的问题,用这个解决了
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sp.edit();
editor.putString(PREF_ACCOUNT_NAME, "cancel");
editor.apply();
this.finish();
...谢谢
答案 2 :(得分:0)
我遇到了同样的问题。对于&#34;取消&#34; action,(responseCode!= RESULT_OK)也返回true,所以我在onActivityResult(..)方法中捕获它。这是代码:
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}else if (responseCode == RESULT_OK) {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
}
}
mIntentInProgress = false;
} else {
ParseFacebookUtils.onActivityResult(requestCode, responseCode, data);
}
}