Skip to content

Instantly share code, notes, and snippets.

@bengongon97
Created July 20, 2020 10:22
Show Gist options
  • Save bengongon97/67a1521063b706b160818542833bc858 to your computer and use it in GitHub Desktop.
Save bengongon97/67a1521063b706b160818542833bc858 to your computer and use it in GitHub Desktop.
onStart in MainActivity for AccountKit
@Override
protected void onStart() {
HuaweiIdAuthParams authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();
service = HuaweiIdAuthManager.getService(MainActivity.this, authParams);
Task<AuthHuaweiId> task = service.silentSignIn();
task.addOnSuccessListener(new OnSuccessListener<AuthHuaweiId>() {
@Override
public void onSuccess(AuthHuaweiId userAccount) {
// Obtain the user's HUAWEI ID information.
binding.usernameTextView.setText(userAccount.getDisplayName());
Toast.makeText(MainActivity.this, "Hello " + userAccount.getDisplayName(), Toast.LENGTH_SHORT).show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// The sign-in failed. Try to sign in explicitly using getSignInIntent().
if (e instanceof ApiException) {
ApiException apiException = (ApiException)e;
Log.i("SIGN-IN-FAIL", "sign failed status:" + apiException.getStatusCode());
Intent signInIntent = service.getSignInIntent();
MainActivity.this.startActivityForResult(signInIntent, REQUEST_SIGN_IN_LOGIN);
//goToLoginActivity(); //try this if not directed to LoginActivity upon failure.
//This is a method I wrote to start an intent. Nothing special. ;)
}
}
});
super.onStart();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment