使用 android.accounts 身份验证器时出现问题

发布于 2024-11-09 02:24:46 字数 1397 浏览 4 评论 0原文

我是 android.accounts api 的新手,现在我尝试用它们做一些事情,但出现了一个看似虚拟的问题...
我已经为我的应用程序创建了一个身份验证器,但尚未实现抽象方法。它的图标成功出现在系统“添加帐户”窗口中,我知道当我单击它时,将调用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 = "[email protected]";
    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 方法的问题,因为我评论此语句时没有发生崩溃。

I'm new to the android.accounts apis, and now I'm trying to do something with them but a seemly dummy problem occurs...
I`ve created an Authenticator for my app but did not yet implement the abstract methods. The icon of it successfully appears in the system Add a Account window, and I know that when I click it, the method addAccount in the Authenticator will be invoked.

Now I wish to do some simple thing in this method, and write 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 = "[email protected]";
    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've seen the demo of SampleSyncAdapter, and make moves like it. But I practice using these APIs by directly adding an account.
But system crashed by the line manager.addAccountExplicitly(account, password, null);
What's wrong with it?


Added later:
Exception in system process. System will crash.
NullPointerException throw by AccountManager.
It seems the problem of the addAccountExplicitly method, as I comment this statement no crash happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-11-16 02:24:46

我已经解决了。

原来是Android 2.0的一个bug。
如果您向AccountManager 添加帐户,您还必须在Android 2.0 平台下为该帐户提供一个SynAdapter。但在Android 2.1及以上版本下一切正常。

这是已知问题,请参考:
http://code.google.com/p/android/issues/详情?id=5009

AccountManager 没有 SyncAdapter?

I've worked it out.

It turns out that it's a bug in Android 2.0.
If you add an account to the AccountManager, you must also provide a SynAdapter to the account, under the Android 2.0 platform. But things are all right under Android 2.1 and above.

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

情场扛把子 2024-11-16 02:24:46
 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;
}

更新

身份验证器

这是我的链接 很好的项目,它也有 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" />
 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 apps which works perfectly. the key here is the AccountAuthenticatorActivity that should set the authentication result bundle which should be registered (the sync adapter from android developers has this.

also here is my addAccount method for the 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 a link i used. this is a good project. i believe it is from the last.fm android app. it also has the source code open on git i believe. so try to compare with that.

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文