AccountManager blockingGetAuthToken gets stuck

痞子三分冷 提交于 2019-12-12 20:15:40

问题


I've been using the SampleSyncAdapter as a base to create my own SyncAdapter. It seems to work well to add a new account, but once I want to get the authtoken with AccountManager.blockingGetAuthToken(... it gets stuck and then throws an OperarationCanceledException after a few minutes.

Does anyone have an Idea of what could be wrong here? The code is almost the same as the sample except I am authenticating towards my own server.

05-24 23:00:23.258: ERROR/SyncAdapter(4961): OperationCanceledExcetpion 05-24 23:00:23.258: ERROR/SyncAdapter(4961): android.accounts.OperationCanceledException 05-24 23:00:23.258: ERROR/SyncAdapter(4961): at android.accounts.AccountManager$AmsTask.internalGetResult(AccountManager.java:1255) 05-24 23:00:23.258: ERROR/SyncAdapter(4961): at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1260) 05-24 23:00:23.258: ERROR/SyncAdapter(4961): at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1181) 05-24 23:00:23.258: ERROR/SyncAdapter(4961): at android.accounts.AccountManager.blockingGetAuthToken(AccountManager.java:737)


回答1:


The blockingGetAuthToken method is a helper that calls getAuthToken synchronously.

If you are accessing the network to retrieve the auth token you will be blocked until the request succeeds. You should check that you can access the network resource properly from within your application.




回答2:


originally method is a convenient way to get authtoken instead calling method getAuthToken. have to make sure not in the mainthread by calling methed runOnUIthread. or you can call the default method getAuthToken and using callback for execute next instruction. for the example.

final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, AccountConfig.AUTHTOKEN_TYPE, null, this, null, null);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                Bundle bnd = future.getResult();
                final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 
                if (authtoken == null) { 
                    return;
                }
                 // this callback interface method 
                logoutCallback.onLogoutFinished(authtoken);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();


来源:https://stackoverflow.com/questions/6116987/accountmanager-blockinggetauthtoken-gets-stuck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!