如何在尝试从Account获取AuthToken时摆脱java.lang.IllegalStateException [英] How to get rid of java.lang.IllegalStateException while trying to getAuthToken from Account
本文介绍了如何在尝试从Account获取AuthToken时摆脱java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取帐户的authToken
,但出现此错误:
I am trying to get the authToken
for an account but getting this error:
java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
这是我获取AuthToken的方式:
This is how I'm getting the 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调用的,就像这样:
I'm calling this from my MainActivity like this:
MyAccount account = new MyAccount(getApplicationContext());
String authtoken = account.getAuthToken());
问题
如何调用MyAccount
获取authToken并摆脱异常?
How can I call MyAccount
to get the authToken and get rid of the exception?
推荐答案
使用 AsyncTask ,以便可以在后台线程(doInBackground)中完成这项工作
USe an AsyncTask so that this work may be completed in a background thread (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
}
}
}
用法:new FooTask().execute();
这篇关于如何在尝试从Account获取AuthToken时摆脱java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文