2011-04-21 70 views
14

我想清除應用程序的數據,當用戶從設置應用程序中的Accounts & sync部分手動刪除帳戶。清除帳戶移除數據

我有我自己的AbstractAccountAuthenticator實施,但沒有方法掛鉤在刪除帳戶進程。任何提示?

+0

不確定它是否有任何用處,但你看過廣播動作ACTION_UID_REMOVED? – Squonk 2011-04-21 18:58:14

回答

18

我一直在思考同樣的問題,這裏是我決定的「解決方案」。這不是我稱之爲「正確」的解決方案,但它是我相信您可以使用當前API管理的最佳解決方案。

我在執行的AbstractAccountAuthenticator類的,我已經重寫了getAccountRemovalAllowed功能如下:

@Override 
public Bundle getAccountRemovalAllowed(
     AccountAuthenticatorResponse response, Account account) 
     throws NetworkErrorException { 
    Bundle result = super.getAccountRemovalAllowed(response, account); 

    if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) 
      && !result.containsKey(AccountManager.KEY_INTENT)) { 
     final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 

     if (removalAllowed) { 
      // Do my removal stuff here 
     } 
    } 

    return result; 
} 

有一個微小的機會,當你從getAccountRemovalAllowed返回去除可能會失敗,但它是可以忽略不計(恕我直言)

由於MisterSquonk建議有,你可以聽(ACCOUNTS_CHANGED_INTENT),但不幸的是,這是廣播時的帳戶改變,而不僅僅是當一個帳戶是刪除的意圖。

我不明白爲什麼這不是SDK的一部分,但也許我們都錯過了明顯的東西!現在,我堅持使用這種方法,因爲我需要刪除我自己的一些數據庫表刪除帳戶。

我希望這會有所幫助。