【问题标题】:prevent showing chooser dialog each time get user account防止每次获取用户帐户时显示选择器对话框
【发布时间】:2017-05-25 06:17:45
【问题描述】:

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

【问题讨论】:

    标签: android user-accounts android-intent-chooser


    【解决方案1】:

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

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

    从偏好中检索数据:

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

    【讨论】:

    • 我需要控制保存的用户(最后检查的用户)是否是运行应用程序的同一用户,这就是为什么我每次都需要检查它,但不希望每次都选择器
    • 然后只需将您当前的用户与之前检查的用户进行比较。如果相同则不显示,否则显示。!
    • 这就是问题所在!当我想获取当前用户时出现选择器。我只需要在获取用户时如何不显示?
    • 你到底想做什么。? @sasan
    • 我想知道用户在使用什么应用
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2014-01-07
      • 2019-04-11
      相关资源
      最近更新 更多