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 (エクストラあり) }}]
私の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 がリダイレクトを処理するようにコードを改善する必要がありますか?