2013-03-11 122 views
8

我試圖在AccountManager中刪除自定義帳戶。如何在Android的AccountManager中刪除帳戶

這是我的代碼:

final Handler handler = new Handler(); 

AccountManagerCallback<Boolean> callback = new AccountManagerCallback<Boolean>() 
     { 
      @Override 
      public void run(AccountManagerFuture<Boolean> arg0) 
      { 
       String test = "test"; 
      } 
     }; 

AccountManagerFuture<Boolean> bool = am.removeAccount(account, callback, handler); 

許可我使用:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission> 
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"></uses-permission> 

的賬戶永遠不會被刪除,並且回調從來不叫,任何想法?在日誌中沒有一絲

+0

你能解決這個我現在面臨一個類似的問題 – 2014-09-15 11:24:08

回答

11

試試這個它會工作

// Global Variables 
    public static final String AUTHORITY = "com.example.package"; 
    public static final String ACCOUNT_TYPE = "com.example.package"; 
    public static final String ACCOUNT = "my_custom_account_name"; 

    // Account Manager definition 
    AccountManager accountManager = (AccountManager) this.getSystemService(ACCOUNT_SERVICE); 

    // loop through all accounts to remove them 
    Account[] accounts = accountManager.getAccounts(); 
    for (int index = 0; index < accounts.length; index++) { 
    if (accounts[index].type.intern() == AUTHORITY) 
     accountManager.removeAccount(accounts[index], null, null); 
    } 

需要

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> 
+0

謝謝,但它不工作 你基本上做的?。同樣的事情我。 的removeAccount()函數不會做任何事情(和BTW返回任何結果)。 – Vico 2013-03-11 12:38:44

+0

@ user2083698你可能會做錯事了完美的作品在我的應用程序 – DjHacktorReborn 2013-03-11 12:41:44

+0

@ user2083698請確保您有上面的權限和你的權威如果在添加 – DjHacktorReborn 2013-03-11 12:43:57

0

此代碼的工作就像一個魅力對我來說。

您將需要WRITE_SYNC_SETTINGS,也需要添加android.permission.MANAGE_ACCOUNTS爲我工作與相同的代碼模式。允許。因此,如果您正確使用AccountManager和帳戶,您將成功刪除該帳戶。

我不得不使用在Android模擬器的客戶經理,所以嘗試測試在真實設備上的一些問題...

AccountManager accMgr = AccountManager.get(this); 
final Account account = new Account(username, accountType); 

removeCaxtonAccount(accMgr, account); 

public void removeCaxtonAccount(AccountManager accMgr, Account account){ 
     accMgr.removeAccount(account, null,null); 
    } 
5

您需要從重寫以下方法在驗證器類AbstractAccountAuthenticator

public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account) { 
    Bundle result = new Bundle(); 
    boolean allowed = true; // or whatever logic you want here 
    result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, allowed); 
    return result; 
} 
2

您必須在removeAccount方法之前調用以下2個方法,並且系統將允許您刪除帳戶管理器中的帳戶。 clearPassword invalidateAuthToken

基於對removeAccount方法說明:

「的鑑別器可以有自己的政策,防止帳戶刪除,在這種情況下,帳戶將不會被刪除。」

玩得開心。

0

這是我的解決方案。我發現的以前的解決方案並沒有明確地等待帳戶的刪除完成,因此他們隨機失敗。

final AccountManager accountManager = AccountManager.get(getContext()); 
    final String accountType = AuthenticatorService.ACCOUNT_TYPE; 

    final Account[] availableAccounts = accountManager.getAccountsByType(accountType); 
    for (final Account availableAccount : availableAccounts) { 
     final AccountManagerFuture<Boolean> booleanAccountManagerFuture = accountManager.removeAccount(availableAccount, null, null); 
     assertTrue("Impossible to delete existing account for this application", booleanAccountManagerFuture.getResult(1, TimeUnit.SECONDS)); 
    } 

注意:您仍然需要前面提到的權限。

0

有同樣的問題

if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP_MR1) { 
      accountManager.removeAccount(account, {}, AContext.app.mainHandler) 
     } else { 
      accountManager.removeAccountExplicitly(account) 
     } 

對於API 22個更高的作品完美,但在API 19並沒有在所有的工作。

終於發現了問題我在執行AbstractAccountAuthenticator的:

override fun getAccountRemovalAllowed(response: AccountAuthenticatorResponse?, account: Account?): Bundle { 
    AccountHelper.removeAccount() 
    return super.getAccountRemovalAllowed(response, account) 
} 

它成爲刪去「AccountHelper後,開始工作。removeAccount()」

我不知道 - 也許它會幫助

+2

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](https://stackoverflow.com/questions/ask)來提問。您可以[添加賞金](https://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](https://stackoverflow.com/help/)後吸引更多關注此問題什麼聲譽)。 - [來自評論](/ review/low-quality-posts/19035101) – 2018-03-07 16:12:48

+0

找到解決方案,編輯帖子 – 2018-03-08 08:20:18