4

この問題をグーグルで検索しましたが、解決策が見つかりませんでした。

独自のカスタム アカウントを作成しました。次のコードを使用してプログラムでアカウントを削除しようとすると、アカウントが削除されません。

Account systemAccount = new Account(mainAccount.getDisplayName(), 
                                    getResources().getString(R.string.account_type));
AccountManager.get(Accounts.this).removeAccount(systemAccount, null, null);

設定からアカウントを削除しようとしても、何も起こりませんでした。アプリケーションをアンインストールしたときにのみ、アカウントが削除されます。

私は何をすべきか?

4

2 に答える 2

4

Future渡された をAccountManagerCallback<Boolean>#runメソッドのパラメーターとして使用していません。

コールバックを 2 番目のパラメーターとして次のように指定する必要があります。public AccountManagerFuture<Boolean> removeAccount (Account account, AccountManagerCallback<Boolean> callback, Handler handler)

myAccountManager.removeAccount(myAccount, new AccountManagerCallback<Boolean>() {
    @Override
    public void run(AccountManagerFuture<Boolean> future) {
        // This is the line that actually starts the call to remove the account.
        boolean wasAccountDeleted = future.getResult();
    }
}, null);

の呼び方には注意が必要ですfuture.getResult()。メイン UI スレッドでは呼び出さないでください。この例では、簡潔にするためにそのメカニズムを提供していません。

于 2014-01-16T03:45:48.513 に答える
2

2つのこと:

  1. アカウント オブジェクトを変更するには、常に からアカウント オブジェクトを取得しAccountManagerてください。

    final AccountManager accountManager = AccountManager.get(this);
    accountManager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
    
  2. をオーバーライドgetAccountRemovalAllowedしている場合Authenticatorは、ブール値 true で Bundle を返していることを確認してください。これがデフォルトの動作です。

    public Bundle getAccountRemovalAllowed(
            AccountAuthenticatorResponse response, Account account)
            throws NetworkErrorException {
        final Bundle result = new Bundle();
    
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
    
        return result;
    }
    
于 2013-10-04T17:29:18.677 に答える