I need to check user account in my app and I need to check it more than once in app , I used this answer and works fine , but the problem is each time I want to get account the chooser dialog appears for " choose an account " and its not good at all , I appreciate any solution for stopping that !
Asked
Active
Viewed 268 times
2 Answers
1
use Shared preferences Store checked value and then check it if exists then dnt show if not then show.!
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "user1");
editor.putInt("idName", 1);
editor.commit();
Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
//Call this function in you OnCreate Method.!
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
Log.e(TAG, "display name: " + acct.getDisplayName());
personName = acct.getDisplayName();
email = acct.getEmail();
if(acct.getPhotoUrl()!= null) {
personPhotoUrl = acct.getPhotoUrl();
}
else
{
personPhotoUrl=Uri.parse("android.resource://com.compscitutorials.basigarcia.navigationdrawervideotutorial/drawable/add_profile_image");;
Log.d(TAG, "handleSignInResult: Photo Url is empty");
}
Log.e(TAG, "Name: " + personName + ", email: " + email
+ ", Image: " + personPhotoUrl.getPath().toString());
// txtName.setText(personName);
// txtEmail.setText(email);
// Glide.with(getApplicationContext()).load(personPhotoUrl)
// .thumbnail(0.5f)
// .crossFade()
// .diskCacheStrategy(DiskCacheStrategy.ALL)
// .into(imgProfilePic);
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
now compare your current user with previous.!
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
if(!currentName.equals(name))
{
//Next, launch the account chooser intent:
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},
false, null, null, null, null);
try {
startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
// This device may not have Google Play Services installed.
// TODO: do something else
}
}
} else {
// Display Toast SignUp Failed
Toast.makeText(this, "SignUp Error", Toast.LENGTH_SHORT).show();
}
}
//Finally, override onActivityResult to get the account type and account name:
@ @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
Atif AbbAsi
- 4,649
- 4
- 21
- 45
-
I need to control if saved user (last checked user) is the same user running app ,that's why I need to check it each time , but don't want the chooser every time – sasan May 25 '17 at 06:33
-
then simply compare your current user with previous checked user. if same then dnt show if not then show.! – Atif AbbAsi May 25 '17 at 06:35
-
that's the problem !!! chooser appears when I want to get current user . All I need is how not to show while getting user ? – sasan May 25 '17 at 06:40
-
what actually u want to do.? @sasan – Atif AbbAsi May 25 '17 at 06:51
-
I want to know what user is using app – sasan May 25 '17 at 06:53
0
you are using account picker so you should not expect that dialog not appear