【问题标题】:Android AccountManager APIAndroid AccountManager API
【发布时间】:2011-05-10 03:17:28
【问题描述】:

我很难理解 Android AccountManager API。就我的想法而言,我可以使用blockingGetAuthToken方法并指定Android是否应该为用户提供通知以允许或拒绝请求。另一种可能性是使用 getAuthToken 并检查是否返回了 KEY_INTENT。如果是这种情况,我可以启动一个新的 Activity,用户可以在其中确认我的请求。

我的问题是我想从服务中调用这两种方法之一。一旦用户做出决定,是否有机会获得回调?

感谢您的帮助

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您想在用户做出决定后进行回调,最好使用异步版本:

    AccountManager mgr = AccountManager.get(getApplicationContext());
    Account[] accounts = mgr.getAccountsByType("com.mydomain");
    // assert that accounts is not empty
    

    您需要使用AccountManagerFuture<Bundle> 来保存身份验证令牌的结果。这必须是异步的,因为 Android 设备可能会同时要求用户登录:

    private AccountManagerFuture<Bundle> myFuture = null;
    private AccountManagerCallback<Bundle> myCallback = new AccountManagerCallback<Bundle>() {
        @Override public void run(final AccountManagerFuture<Bundle> arg0) {
            try {
               myFuture.getResult().get(AccountManager.KEY_AUTHTOKEN); // this is your auth token
           } catch (Exception e) {
               // handle error
           }
       }
    

    }

    现在您可以异步请求身份验证令牌:

    myFuture = mgr.getAuthToken(accounts[0], AUTH_TOKEN_TYPE, true, myCallback, null);
    

    【讨论】:

    • 如果您想要更全面的指南,请访问:udinic.wordpress.com/2013/04/24/…
    • @Udinic 你能帮助如何异步获取身份验证令牌以将其作为 http 请求的参数传递吗?当我使用blockingGetAuthToken调用一段时间时,应用程序崩溃了!我必须为每个 blockingGetAuthToken 调用调用异步任务?? paste.ubuntu.com/6245277 简而言之,我无法将 void getTokenForAccountCreateIfNeeded 更改为 String getTokenForAccountCreateIfNeeded
    • @LOG_TAG 我不明白这个问题。为什么会崩溃?您收到的错误信息是什么?到目前为止,您尝试过什么?
    • @Udinic 由于 UI 踏板上的负载而解决了这个问题,现在我正在通过异步任务调用同步阻塞GetAuthToken
    • @Udinic 一个!!也解决了与 android:accountType="com.myapp.am" 和 AccountGeneral 类的 public static final String ACCOUNT_TYPE = "com.test.app"; 的冲突请提及我的博客! :) 我花了 3 小时来解决 :) 如果它错过匹配它会调用活页夹错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多