防止每次获取用户帐户时显示选择器对话框

prevent showing chooser dialog each time get user account

我需要在我的应用程序中检查用户帐户,我需要在应用程序中多次检查它,我使用 并且工作正常,但问题是每次我想获取帐户选择器出现“选择一个帐户”的对话框,但它一点也不好,我感谢任何阻止它的解决方案!

使用共享首选项存储选中的值,然后检查它是否存在,如果不存在则不显示。!

首选项中的设置值:

// 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();

从首选项中检索数据:

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);
        }
    }

现在将您当前的用户与以前的用户进行比较。!

 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();
            }
        }

//最后重写onActivityResult获取账户类型和账户名:

@   @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);
    }
}

您正在使用帐户选择器,因此您不应期望该对话框不会出现