自定义 AccountAuthenticator:获取授权令牌

Custom AccountAuthenticator: auth token getting

当我尝试从 AccountManager 获取身份验证令牌时,我需要调用

AccountManagerFuture<Bundle> getAuthToken(Account account, String authTokenType, Bundle options, Activity activity, AccountManagerCallback<Bundle> callback, Handler handler);

AccountManagerFuture<Bundle>AccountManagerCallback<Bundle> 有什么区别?我看了文档,但不是很清楚。

P.S。也许,这是菜鸟问题,但我真的无法从 Google 文档中理解它。

据我了解,AccountManagerCallback 是一个可选的回调,用于在调用结果准备就绪时通知您。

由于 AccountManagerFuture<Bundle> 没有任何机制可以在结果准备就绪时通知您,因此您必须调用 getResult() or getResult(long, TimeUnit) to get the result. Since these calls are blocking you would need to spawn a new thread first. The documentation explicitly states that you must not call them from the main thread. Of course it should be safe to use them on the main thread once the isDone() 方法 returns true .

回调仅收到一个 AccountManagerFuture<Bundle>,其中还包含结果(实际上可能与上面的对象相同)但已经完成加载。这是一种在结果准备就绪时得到通知的便捷方法,您可以安全地调用 AccountManagerFuture.getResult(),而无需等待结果,也不必自己生成线程。