標籤:android style http io 使用 ar 檔案 sp 資料
我順帶把AbstractAccountAuthenticator 也翻譯了,感覺直接看SampleSyncAdapter例子很難快速明白整體的意圖,配合api是個好的方式,感歎android的api這是太詳細了。
AbstractAccountAuthenticator 概述
AbstractAccountAuthenticator 概述
這是一個抽象的基類,用於建立賬戶管理器(AccountAuthenticators)。為了成為一 個 認證器,一個類必須繼承該類,提供抽象方法的實現,並且寫一個服務(service),
在被ACTION_AUTHENTICATOR_INTENT作為action的intent調用時,在該服務的 onBind(android.content.Intent) 方法實現中,直接返回getIBinder() 的傳回值結果。在
AndroidManifest.xml 檔案中,這個服務必須指定下面的 intent過濾器(intent filter )和中繼資料標記。
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
譯者註:ACTION_AUTHENTICATOR_INTENT其實是個常量,等於字串android.accounts.AccountAuthenticator,其實就是和上面這個intent filter的過濾器action相同。
上面的xml描述中,android:resource 屬性必須指向一個資源檔,像下面這樣:
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="typeOfAuthenticator"
android:icon="@drawable/icon"
android:smallIcon="@drawable/miniIcon"
android:label="@string/label"
android:accountPreferences="@xml/account_preferences"
/>
使用你自己的資源替換 icon 和 label 屬性指向的值。android:accountType 屬性必須是個字串,它唯一標識了你的 認證器,並且和 使用者使用AccountManager 調用時
指定的字串相同,同時 和你的賬戶類型(account type)一致。 android:icon的一個使用者是在 “賬戶和同步”設定頁,android:smallIcon的一個使用者是在 連絡人
應用程式的標籤面板。
android:accountPreferences屬性指向一個 喜好設定螢幕設定的xml設定檔(PreferenceScreen xml ),它包含了一個PreferenceScreen 的列表,可以層級嵌套。
它可以被調用以管理認證器。樣本如下:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/title_fmt" />
<PreferenceScreen
android:key="key1"
android:title="@string/key1_action"
android:summary="@string/key1_summary">
<intent
android:action="key1.ACTION"
android:targetPackage="key1.package"
android:targetClass="key1.class" />
</PreferenceScreen>
</PreferenceScreen>
一些抽象方法的標準實現模式,像下面這樣:
- * 如果為 認證器 提供的參數是足夠的,到達了完全的滿意,這時將會這樣做(will do so )並且返回一個包含了結果的Bundle。
- * 如果 認證器 需要 從使用者那裡收集資訊才能達到滿意,這時,將建立一個intent開啟“提示使用者資訊的activity”,並且完成該請求。這個intent必須返回一個包含了 指定key名稱為 KEY_INTENT 的 Bundle.當完成的時候,這個activity需要返回final修飾的結果。
這個intent應該使用key指示 KEY_ACCOUNT_MANAGER_RESPONSE來包含AccountAuthenticatorResponse。這個activity在結束時必須調用 onResult(Bundle) 或者 onError(int, String) 。
- 如果認證器不能同步處理請求,並且返回一個結果。那麼當完成請求時,它可以選擇返回null和使用 AccountManagerResponse 去發送結果。
後續的關於 每個抽象認證器方法 的描述,將不描述 可能的非同步原生請求處理,而將描述輸入參數和期望結果來替代。
當寫一個activity去滿足那些請求,一種方式,必須在activity關閉時(或者任何其他情況下activity的作者認為是一個正確的時間去響應時),通過AccountManagerResponse 並且通過響應返回結果。AccountAuthenticatorActivity用於處理這些,那麼當寫activity去處理這些請求時,某人可以希望去繼承(extend)它。
-----
張雲飛vir 寫於 2014-10-15
Android中文翻譯 - AbstractAccountAuthenticator概述