我有一个使用 AccountManager 和自定义 AccountAuthenticator 注册用户的应用程序。
我需要实现该Log Out
功能。
当用户单击Log Out
按钮时,他们的帐户被成功删除,但活动保持不变,用户不会被重定向到AccountAuthenticatorActivity
,但如果我关闭应用程序并再次打开它,它将显示身份验证屏幕(即帐户实际上已被删除)。
我的问题:
我是否必须自己执行重定向(使用finish(); startActivity(...);
)或 Authenticator 和 AccountManager 应该为我处理它(因为我认为如果它被声明为服务,它应该)?
也许我必须实现某种帐户删除侦听器?
无论如何,这是我删除帐户的方法MainActivity
:
private void performLogout() {
Account[] accounts = accountManager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
if (accounts.length != 0) {
accountManager.clearPassword(accounts[0]);
accountManager.invalidateAuthToken(AccountGeneral.ACCOUNT_TYPE,
accountManager.getAuthToken(accounts[0], AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, null, true,
accountManagerFuture -> {
try {
Log.d("invalidateAuthToken", accountManagerFuture.getResult().toString());
} catch (android.accounts.OperationCanceledException | AuthenticatorException | IOException e) {
e.printStackTrace();
}
}, null).toString());
if (Build.VERSION.SDK_INT < 23) { // use deprecated method
accountManager.removeAccount(accounts[0], accountManagerFuture -> {
try {
if (accountManagerFuture.getResult()) {
Log.d("Deprecated ACCOUNT REMOVAL", "ACCOUNT REMOVED");
}
} catch (android.accounts.OperationCanceledException | IOException | AuthenticatorException e) {
e.printStackTrace();
}
}, null);
} else {
accountManager.removeAccount(accounts[0], this, accountManagerFuture -> {
try {
if (accountManagerFuture.getResult() != null) {
Log.d("ACCOUNT REMOVAL", "ACCOUNT REMOVED");
}
} catch (android.accounts.OperationCanceledException | AuthenticatorException | IOException e) {
e.printStackTrace();
}
}, null);
}
}
}
顺便说一句,当我单击时Log Out
,我可以在日志中看到以下行:
D/invalidateAuthToken: Bundle[{intent=Intent { cat=[2] cmp=discounty.com/.activities.LoginActivity (has extras) }}]
所以看起来我的LoginActivity
(AccountAuthenticatorActivity)实际上想要出现,但有些东西阻止它这样做。
在我的自定义AccountAuthenticator
中,我实现了这个方法(以及其他几个负责帐户和令牌创建的方法):
@Override
public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account) throws NetworkErrorException {
Bundle bundle = new Bundle();
boolean allowed = true;
bundle.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, allowed);
return bundle;
}
在我的清单中,我以以下方式声明了服务:
<service
android:name=".authenticator.DiscountyAuthenticationService"
android:process=":auth">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
那么,我是否必须手动重定向用户,或者我需要改进代码中的某些内容以使 AccountManager 为我处理重定向?