你好呀!
我正在尝试使用 Google 设置此帐户登录,但我现在在此帐户设置方面遇到了很多麻烦:“该帐户未注销”。如果帐户已记录,我正在尝试显示一个对话框以确认响应,而不是显示一个对话框以确认“退出”,如果未记录则它将显示“Google 登录”。
(1) Screen_SignIn。
(2) Screen_Profile。
1º 如果我登录“Screen_SignIn”。我只能在“Screen_Profile”中注销一次。
2º 问题之一是,如果我在“Screen_Profile”中登录。我无法注销,它只是在应用程序完成后注销;
1º[CODE] Screen_SignIn。
public class SignInScreen extends AppCompatActivity {
private TextView textView;
private Button signInButton;
private final int RC_SIGN_IN = 101;
private final String TAG = "SIGN-IN ACTIVITY";
private GoogleSignInClient googleSignInClient;
private GoogleSignInAccount googleSignInAccount;
private Intent intent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sing_in);
signInButton = findViewById(R.id.sign_in_button);
textView = findViewById(R.id.textView_Skip_Sign);
setDefaultClientLogin();
setOnClickListener();
}
private void setOnClickListener() {
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
Log.e(TAG, "googleSignInClient.getSignInIntent()");
}
});
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(v.getContext(), Main.class);
startActivity(intent);
finish();
}
});
}
private void setDefaultClientLogin() {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by googleSignInOptions.
googleSignInClient = GoogleSignIn.getClient(SignInScreen.this, googleSignInOptions);
}
private void signIn() {
intent = googleSignInClient.getSignInIntent();
/* registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (result.getResultCode() == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
handleSignInResult(task);
}
}
}).launch(intent);*/
startActivityForResult(intent,RC_SIGN_IN);
}
@Override
protected void onStart() {
super.onStart();
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
//The method will check if the account is null or not
//if null the user will sign in, else the user will enter the app
updateUI(account);
}
private void updateUI(GoogleSignInAccount account) {
if (account != null) {
intent = new Intent(getApplicationContext(), Main.class);
startActivity(intent);
finish();
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
///IF TEH CONNECTION FAILD THE APP SHOULD BE TESTING THE CONNECTION ETC
updateUI(null);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
}
2º[CODE] Screen_Profile。
public class Main extends AppCompatActivity {
private DrawerLayout drawerLayout;
private LinearLayout linearLayout_boxInfo;
private ImageView imageView_profile;
private TextView textView_name_account, textView_email_account;
private final int RC_SIGN_IN = 111;
private GoogleSignInOptions googleSignInOptions;
private GoogleSignInClient googleSignInClient;
private GoogleSignIn googleSignIn;
private GoogleSignInAccount account;
private Intent intent;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawerLayout);
NavigationView navigationView = findViewById(R.id.navigationView);
imageView_profile = navigationView.getHeaderView(0).findViewById(R.id.imageView_User_Photo);
linearLayout_boxInfo = navigationView.getHeaderView(0).findViewById(R.id.linearLayout_infobox);
textView_name_account = navigationView.getHeaderView(0).findViewById(R.id.textView_name_account);
textView_email_account = navigationView.getHeaderView(0).findViewById(R.id.textView_email_account);
setInitialConfiguration();
setOnClickListener();
setFragmentView(new MainFragment());
}
private void setInitialConfiguration() {
//Initiate the drawers as closed
drawerLayout.closeDrawers();
setDefaultClientLogin();
}
private void setOnClickListener() {
linearLayout_boxInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (account == null) {
Toast.makeText(v.getContext(), "ACCOUNT == NULL, signIn();", Toast.LENGTH_SHORT).show();
signIn();
} else {
Toast.makeText(v.getContext(), "ACCOUNT != NULL, LOGOUT DIALOG;", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(Main.this)
.setTitle(getResources().getString(R.string.title_dialog_logout))
.setMessage(getResources().getString(R.string.message_logout_account))
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
googleSignInClient.signOut().addOnCompleteListener(Main.this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
if (account.getAccount() != null) {
Toast.makeText(getApplicationContext(), "TASK, COMPLETE!, FAIL TO LOGOUT!", Toast.LENGTH_LONG).show();
} else {
updateUI(account);
}
} else {
Toast.makeText(getBaseContext(), "FAIL! TO LOGOUT", Toast.LENGTH_LONG).show();
}
}
});
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
}
});
}
private void setDefaultClientLogin() {
//Create new Settings to the Client
googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
//The Client receive the Settings From GoogleSignIn
googleSignInClient = GoogleSignIn.getClient(Main.this, googleSignInOptions);
}
private void signIn() {
intent = googleSignInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
private void updateUI(GoogleSignInAccount account) {
if (account != null) {
textView_name_account.setText(account.getDisplayName());
textView_email_account.setText(account.getEmail());
Picasso.get().load(account.getPhotoUrl()).error(android.R.drawable.sym_def_app_icon).placeholder(android.R.drawable.sym_def_app_icon).into(imageView_profile);
} else {
//account == null
textView_name_account.setText("Not Logged");
textView_email_account.setText("Try to log into your account!");
imageView_profile.setImageResource(android.R.drawable.sym_def_app_icon);
}
}
@Override
protected void onStart() {
super.onStart();
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
//The method will check if the account is null or not
//if null the user will sign in, else the user will enter the app
updateUI(account);
}
private void setFragmentView(Fragment fragment) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainerView, fragment);
fragmentTransaction.commit();
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("Error", "signInResult:failed code=" + e.getStatusCode());
///IF TEH CONNECTION FAILD THE APP SHOULD BE TESTING THE CONNECTION ETC
updateUI(null);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
}