尝试从 Account 获取AuthToken 时如何摆脱 java.lang.IllegalStateException

2023-12-20

我正在尝试获取authToken对于一个帐户但出现此错误:

java.lang.IllegalStateException: calling this from your main thread can lead to deadlock

这就是我获取 AuthToken 的方式:

public class MyAccount {
 private final Account account;
 private final AccountManager manager;
 private final Context context;

 public MyAccount (Context context) {
        this.context = context;
        final Account[] accounts = AccountManager.get(context).getAccountsByType("com.myapp");
        account = accounts.length > 0 ? accounts[0] : null;
        manager = AccountManager.get(context);
 }

    public String getAuthToken() {
        @SuppressWarnings("deprecation")
        AccountManagerFuture<Bundle> future = manager.getAuthToken(account,"com.myapp", false, null, null);

        try {
            //GETTING EXCEPTION HERE
            Bundle result = future.getResult();
            return result != null ? result.getString(KEY_AUTHTOKEN) : null;
        } catch (AccountsException e) {
            Log.e(TAG, "Auth token lookup failed", e);
            return null;
        } catch (IOException e) {
            Log.e(TAG, "Auth token lookup failed", e);
            return null;
        }
    }
}

我从我的 MainActivity 中调用它,如下所示:

 MyAccount account = new MyAccount(getApplicationContext());
 String authtoken = account.getAuthToken());

Question

我怎样才能打电话MyAccount获取 authToken 并摆脱异常?


USe an 异步任务 http://developer.android.com/reference/android/os/AsyncTask.html这样这项工作就可以在后台线程中完成(doInBackground)

private class FooTask extends AsyncTask<Void, Void, String>{
    /**
     * Override this method to perform a computation on a background thread. The
     * specified parameters are the parameters passed to {@link #execute}
     * by the caller of this task.
     *
     * This method can call {@link #publishProgress} to publish updates
     * on the UI thread.
     *
     * @param params The parameters of the task.
     *
     * @return A result, defined by the subclass of this task.
     *
     * @see #onPreExecute()
     * @see #onPostExecute
     * @see #publishProgress
     */
    @Override
    protected String doInBackground(Void... params) {
        MyAccount account = new MyAccount(getApplicationContext());
        String authtoken = account.getAuthToken());
        return authtoken;
    }

    /**
     * <p>Runs on the UI thread after {@link #doInBackground}. The
     * specified result is the value returned by {@link #doInBackground}.</p>
     *
     * <p>This method won't be invoked if the task was cancelled.</p>
     *
     * @param result The result of the operation computed by {@link #doInBackground}.
     *
     * @see #onPreExecute
     * @see #doInBackground
     * @see #onCancelled(Object)
     */
    @Override
    protected void onPostExecute(String token) {
        if (token != null){
            //use token here
        }
    }
}

Usage : new FooTask().execute();

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试从 Account 获取AuthToken 时如何摆脱 java.lang.IllegalStateException 的相关文章

随机推荐