Android Account Authenticator edit Email Id Credentials

和自甴很熟 提交于 2019-12-10 12:37:45

问题


When i login with test1@gmail.com in my application.It generate account successfully with my email like this

Now i logout and login with different email like test2@gmail.com then it generate account like this

I want to know that which is the best way

1) Remove first account and add second account

2) Update first account with second if it is possible to update it.


What is Problem I am getting actually?

If I remove and again add account using addAccountExplicitly it takes some time for creating new account so my next code is executed and account returns null.

Is is possible to update account using the help of updateCredentials if yes then how ??

Edited:

What I do actually?

  • Creating bundle with required data for account

  • Checking whether account already exists with local inserted bundle params "global_user_id", If it is already exists then I have to update EMAIL which is used as login (see above images.)

  • Currently I am doing as, Removing old account and Adding new Account, but next line is for SyncAdapter Configuration which needs account. In that getting NULL because adding account takes some time in background.

Is there any other solution to update that Email Id?


回答1:


I got solution of this problem.

Question: Removing/Adding Account remains null account object

Solution 1:

First I removed account using removeAccount() and then tried to addAccountExplicitly BUT removeAccount() taking time to execute in background thread while addAccountExplicitly called and doing further process.

So I changed my flow, as I used removeAccount method of AccountManager class and doing whole process in that handler, So I write my code inside the callback area.

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
     mAccountManager.removeAccount(accounts[0], LoginActivity.this, new AccountManagerCallback<Bundle>() {
         @Override
         public void run(AccountManagerFuture<Bundle> future) {

             // Creating the account on the device and setting the auth token we got
             // (Not setting the auth token will cause another call to the server to authenticate the user)
             mAccountManager.addAccountExplicitly(account, accountPassword, intent.getBundleExtra(AccountManager.KEY_USERDATA));
             mAccountManager.setAuthToken(account, authTokenType, authToken);

             /**
              * Setting for Sync Adapter
              * Syncing Configuration
              */
              SyncAdapter.configSyncAdapter(mContext);
        }
    }, null);

} else {

    mAccountManager.removeAccount(accounts[0], new AccountManagerCallback<Boolean>() {
        @Override
        public void run(AccountManagerFuture<Boolean> future) {

            // Creating the account on the device and setting the auth token we got
            // (Not setting the auth token will cause another call to the server to authenticate the user)
            mAccountManager.addAccountExplicitly(account, accountPassword, intent.getBundleExtra(AccountManager.KEY_USERDATA));
            mAccountManager.setAuthToken(account, authTokenType, authToken);

            /**
             * Setting for Sync Adapter
             * Syncing Configuration
             */
             SyncAdapter.configSyncAdapter(mContext);
        }
    }, null);
}

Solution 2:

I found method called renameAccount() but it require minimum sdk version 21. As per docs:

Rename the specified Account. This is equivalent to removing the existing account and adding a new renamed account with the old account's user data.

It is safe to call this method from the main thread.

Thank you.




回答2:


Problem

when you create/remove account, it executes that task in different thread(background thread) and thus it goes to next line which may have null value.

Solution

Step 1. you should use addOnAccountsUpdatedListener for receiving callback to your main thread.

Step 2. you will receive callback on onAccountsUpdated of OnAccountsUpdateListener

Step 3. Once you receive callback, you may put your next code over there inside that method. i.e.: SyncAdapter configuration

Step 4. Don't forget to get rid of listener which you had registered, otherwise you will suffer from memory leak. So use removeOnAccountsUpdatedListener once you're done.

I hope it will be helpful !




回答3:


I think you should delete first account and then add the new one. As far as your problem about the code which executes before your account is executed you can control this by

AccountManager accountManager = AccountManager.get(this); //this is Activity
Account account = new Account("MyAccount","com.company.demo.account.DEMOACCOUNT");
boolean success = accountManager.addAccountExplicitly(account,"password",null);
if(success){
    Log.d(TAG,"Account created");
}else{
    Log.d(TAG,"Account creation failed. Look at previous logs to investigate");
}

Just check the success boolean. And do your work accordingly :)



来源:https://stackoverflow.com/questions/42731571/android-account-authenticator-edit-email-id-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!