使用 android.accounts 身份验证器时出现问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经解决了。
原来是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?
我在我的一个应用程序中使用此代码,效果非常好。这里的关键是AccountAuthenticatorActivity,它应该设置应该注册的身份验证结果包(Android开发人员的同步适配器有这个。
这也是我的accountAuthentication服务的addAccount方法
更新
身份验证器
这是我的链接 很好的项目,它也有 git 上开放的源代码。
我相信这是一个
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
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