使用android.accounts身份验证器的问题

时间:2011-05-19 10:42:58

标签: android authentication accounts

我是android.accounts apis的新手,现在我正在尝试用它们做些什么,但是出现了一个似乎很小的问题...
我为我的应用程序创建了一个Authenticator,但还没有实现抽象方法。它的图标成功出现在系统“添加帐户”窗口中,我知道当我单击它时,将调用Authenticator中的方法addAccount。

现在我希望在这个方法中做一些简单的事情,并在下面编写代码:

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

我已经看过SampleSyncAdapter的演示了,并且做了类似的动作。但我通过直接添加帐户来练习使用这些API。 但系统因行manager.addAccountExplicitly(account, password, null);而崩溃 怎么了?


稍后添加: 系统过程中的例外情况。系统会崩溃。 由AccountManager抛出NullPointerException。 似乎是addAccountExplicitly方法的问题,因为我评论此语句没有发生崩溃。

2 个答案:

答案 0 :(得分:1)

我已经解决了。

事实证明,这是Android 2.0中的一个错误 如果您向AccountManager添加帐户,必须还在Android 2.0平台下为该帐户提供SynAdapter。但在Android 2.1及更高版本下,一切都很好。

这是一个已知问题,请参阅:
http://code.google.com/p/android/issues/detail?id=5009

AccountManager without a SyncAdapter?

答案 1 :(得分:0)

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

我在我的一个应用程序中使用此代码,该代码完美无缺。这里的关键是AccountAuthenticatorActivity应该设置应该注册的身份验证结果包(Android开发人员的同步适配器有这个。

这里也是accountAuthentication服务的addAccount方法

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

<强>更新

Authenticator

这是我使用的链接。这是一个很好的项目。我相信它来自last.fm安卓应用。它也有git上的源代码打开我相信。所以试着与之比较。

<强>权限

<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" />
相关问题