Android如何从SyncAdapter执行任何ui操作?
我已经完成了与syncAdapter相关的所有操作,但现在我遇到了一个小问题 身份验证令牌 2小时后,我的令牌刚刚过期,然后我需要向用户显示一个对话框,再次输入他的密码,以便他可以续订令牌Android如何从SyncAdapter执行任何ui操作?,android,synchronization,Android,Synchronization,我已经完成了与syncAdapter相关的所有操作,但现在我遇到了一个小问题 身份验证令牌 2小时后,我的令牌刚刚过期,然后我需要向用户显示一个对话框,再次输入他的密码,以便他可以续订令牌 AccountManager.get(getContext()).getAuthToken(account, LoginActivity.ACCOUNT_TYPE, null, false, new AccountManagerCallback<Bundle>() { @
AccountManager.get(getContext()).getAuthToken(account, LoginActivity.ACCOUNT_TYPE, null, false, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> arg0) {
try {
arg0.getResult();
} catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, null);
AccountManager.get(getContext()).getAuthToken(account,LoginActivity.account_TYPE,null,false,new AccountManager callback()){
@凌驾
公共作废运行(AccountManagerFuture arg0){
试一试{
arg0.getResult();
}捕捉(操作取消异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(认证异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
},空);
我正在onPerformSync上运行此操作,但这不是打开活动。执行此操作有两个部分 1) 在您的
AbstractThreadedSyncAdapter
重写的onPerformSync
方法的实现中,您需要
- 使用方法
blockingGetAuthToken()
- 尝试使用authcode执行同步过程(即web服务调用或任何您使用它的用途)
- 如果上一步失败是因为authcode已过期(例如,您的web服务返回某种类型的authcode过期消息),则需要通过
使用方法AccountManager
invalidateAuthToken()
getAuthToken()
方法的AbstractAccountAuthenticator
实现中
- 使用
检索用户上次提供的密码,并尝试使用这些凭据从web服务获取新的身份验证码AccountManager
- 如果上一步失败,则将打开登录活动的意图添加到从
方法返回的捆绑包中这将导致登录屏幕显示getAuthToken()
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse oResponse, Account oAccount, String strAuthTokenType, Bundle options)
throws NetworkErrorException {
// Validate the authentication type
if (!strAuthTokenType.equals("TODO: your auth token type URL here"))
{
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
return result;
}
// Try to get the password already stored in account manger, if there is one
final AccountManager oAccountManager = AccountManager.get(moContext);
final String strPassword = oAccountManager.getPassword(oAccount);
if (strPassword != null)
{
// TODO: Call the authentication web service method to get a fresh authcode
// Pass the strPassword and oAccount.name
Boolean blnVerified = //TODO: were the username + password authenticated?
String strNewAuthCode = //TODO: the new authcode returned by your authentication web service
// If it worked then return the result
if (blnVerified)
{
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, oAccount.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, "TODO: your account type URI here");
result.putString(AccountManager.KEY_AUTHTOKEN, strNewAuthCode);
return result;
}
}
// Password is missing or incorrect. Start the activity to ask user to provide the missing credentials.
// Open a UI form to get the user to input their username and password again
final Intent oIntent = new Intent(moContext, frmAccount_Auth.class);
oIntent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, oResponse);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, oIntent);
return bundle;
}