Problem using android.accounts Authenticator

I'm new to android.accounts apis and now I'm trying to do something with them, but a seemingly bogus problem comes up ...
I've created an authenticator for my app, but haven't implemented abstract methods yet. Its icon appears successfully in the Add Account window, and I know that clicking this method will call the addAccount method in the Authenticator.

Now I want to do a simple thing in this method and write the codes below:

    @Override
public Bundle addAccount(AccountAuthenticatorResponse response,
        String accountType, String authTokenType, String[] requiredFeatures,
        Bundle options) {

    Log.d(LOG_TAG, "RRAuthenticator add account... ");
    String accountName = "example@example.com";
    Account account = new Account(accountName, accountType);
    String password = "example_password";
    AccountManager manager = AccountManager.get(context);
    manager.addAccountExplicitly(account, password, null);

    Bundle bundle = new Bundle();
    bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
    bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
    bundle.putString(AccountManager.KEY_AUTHTOKEN, "example_authtoken");
    return bundle;
}

      

I saw SampleSyncAdapter demo and made moves like this. But I am using these APIs just by adding an account. But the system crashed on line. manager.addAccountExplicitly(account, password, null);

What's wrong with him?


Added later: Exception in system process. The system will fail. NullPointerException error from AccountManager. Apparently the problem is the addAccountExplicitly method, as I comment out this statement, no glitches occur.

+2


source to share


2 answers


I worked.

It turns out this is a bug in Android 2.0.
If you add an account to the AccountManager, you must also provide a SynAdapter for the account under Android 2.0 platform. But everything is fine under Android 2.1 and higher.



This is a known issue, please refer to:
http://code.google.com/p/android/issues/detail?id=5009
and
AccountManager without Sync adapter?

+1


source


 Account account = new Account(username, AuthConstants.ACCOUNT_TYPE);
            if (am.addAccountExplicitly(account, password, null)) {
                result = new Bundle();
                ContentResolver.setSyncAutomatically(account, DB.AUTHORITY, true);
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
                return result;
            } 

      

I am using this code in one of my applications which works great. the key point here is the AccountAuthenticatorActivity, which should set the authentication result set to be registered (this sync adapter from Android developers).

also here's my addAccount method for accountAuthentication service

    @Override
public Bundle addAccount(AccountAuthenticatorResponse response,
    String accountType, String authTokenType, String[] requiredFeatures,
    Bundle options) {
    final Intent intent = new Intent(mContext, LoginScreen.class);
    intent.putExtra(LoginScreen.PARAM_AUTHTOKEN_TYPE,
        authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
        response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

      

UPDATE



Authenticator

Here is the link I used. this is a good project. I believe this is an Android application. it also has source code open on git I believe. so try to compare with this.

PERMISSIONS

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
  <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
  <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
  <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

      

0


source







All Articles