【Android Developers Training】 93. 创建一个空验证器
注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接:http://developer.android.com/training/sync-adapters/creating-authenticator.html
同步適配器框架假定你的同步適配器在同步數(shù)據(jù)時,設備存儲會有一個賬戶,服務器存儲端會有登錄驗證。因此,框架期望你提供一個叫做驗證器的組件作為你的同步適配器的一部分。該組件會植入Android賬戶及認證框架,并提供一個標準的接口來處理用戶憑據(jù),比如登錄信息。
甚至,如果你的應用不使用賬戶,你仍然需要提供一個認證器組件。如果你不使用賬戶或者服務器登錄,認證器所處理的信息將被忽略,所以你可以提供一個認證器組件,它包括了一個空的實現(xiàn)。同時你需要提供一個捆綁的Service,來允許同步適配器框架來調用認證器的方法。
這節(jié)課將向你展示如何定義一個空驗證器的所有滿足其實現(xiàn)要求的部件。如果你想要提供一個真實的處理用戶賬戶的驗證器,可以閱讀:AbstractAccountAuthenticator。
一). 添加一個空驗證期組件
要在你的應用中添加一個空驗證器,創(chuàng)建一個繼承AbstractAccountAuthenticator的類,并將要覆寫的方法置空(這樣就不會做任何處理了),返回null或者拋出異常。
下面的代碼片段是一個空驗證器的例子:
/** Implement AbstractAccountAuthenticator and stub out all* of its methods*/ public class Authenticator extends AbstractAccountAuthenticator {// Simple constructorpublic Authenticator(Context context) {super(context);}// Editing properties is not supported @Overridepublic Bundle editProperties(AccountAuthenticatorResponse r, String s) {throw new UnsupportedOperationException();}// Don't add additional accounts @Overridepublic Bundle addAccount(AccountAuthenticatorResponse r,String s,String s2,String[] strings,Bundle bundle) throws NetworkErrorException {return null;}// Ignore attempts to confirm credentials @Overridepublic Bundle confirmCredentials(AccountAuthenticatorResponse r,Account account,Bundle bundle) throws NetworkErrorException {return null;}// Getting an authentication token is not supported @Overridepublic Bundle getAuthToken(AccountAuthenticatorResponse r,Account account,String s,Bundle bundle) throws NetworkErrorException {throw new UnsupportedOperationException();}// Getting a label for the auth token is not supported @Overridepublic String getAuthTokenLabel(String s) {throw new UnsupportedOperationException();}// Updating user credentials is not supported @Overridepublic Bundle updateCredentials(AccountAuthenticatorResponse r,Account account,String s, Bundle bundle) throws NetworkErrorException {throw new UnsupportedOperationException();}// Checking features for the account is not supported @Overridepublic Bundle hasFeatures(AccountAuthenticatorResponse r,Account account, String[] strings) throws NetworkErrorException {throw new UnsupportedOperationException();} }二). 將驗證器綁定到框架
為了讓同步適配器框架可以訪問你的驗證器,你必須為它創(chuàng)建一個捆綁服務。這一服務提供一個Android?binder對象,允許框架調用你的驗證器,并且在驗證器和框架間傳輸數(shù)據(jù)。
因為框架會在它需要第一次訪問驗證器時啟動Service,你也可以使用服務來實例化驗證器,方法是通過在服務的Service.onCreate()方法中調用驗證器的構造函數(shù)。
下面的代碼樣例展示了如何定義綁定Service:
/*** A bound Service that instantiates the authenticator* when started.*/ public class AuthenticatorService extends Service {...// Instance field that stores the authenticator objectprivate Authenticator mAuthenticator;@Overridepublic void onCreate() {// Create a new authenticator objectmAuthenticator = new Authenticator(this);}/** When the system binds to this Service to make the RPC call* return the authenticator's IBinder.*/@Overridepublic IBinder onBind(Intent intent) {return mAuthenticator.getIBinder();} }三). 添加驗證器的元數(shù)據(jù)文件
要將你的驗證器組件插入到同步適配器和賬戶框架中,你需要為框架提供帶有描述組件的元數(shù)據(jù)。該元數(shù)據(jù)聲明了你創(chuàng)建的同步適配器的賬戶類型以及系統(tǒng)所顯示的用戶接口元素(如果你希望將你的賬戶類型對用戶可見)。在你的項目目錄:“/res/xml/”下,將元數(shù)據(jù)聲明于一個XML文件中。你可以隨便為它起一個名字,一般來說,可以叫“authenticator.xml”
在這個XML文件中,包含單個元素<account-authenticator>,它有下列一些屬性:
android:accountType
同步適配器框架需要每一個適配器以域名的形式擁有一個賬戶類型。框架使用作為其內部的標識。對于需要登錄的服務器,賬戶類型會和賬戶一起發(fā)送到服務端作為登錄憑據(jù)的一部分。
如果你的服務不需要登錄,你仍然需要提供一個賬戶類型。值的話就用你能控制的一個域名即可。由于框架會使用它來管理同步適配器,所以值不會發(fā)送到服務器上。
android:icon
指向一個包含一個圖標的Drawable資源的指針。如果你在“res/xml/syncadapter.xml”中通過指定“android:userVisible="true"”讓同步適配器可見,那么你必須提供圖標資源。它會在系統(tǒng)的設置中的賬戶這一欄內顯示。
android:smallIcon
指向一個包含一個微小版本圖標的Drawable資源的指針。結合具體的屏幕大小,這一資源可能會替代“android:icon”中所指定的圖標資源。
android:label
將指明了用戶賬戶類型的string本地化。如果你在“res/xml/syncadapter.xml”中通過指定“android:userVisible="true"”讓同步適配器可見,那么你需要提供這個string。它會在系統(tǒng)的設置中的賬戶這一欄內顯示,就在你定義的圖標旁邊。
下面的代碼樣例展示了你之前為驗證器創(chuàng)建的XML文件:
<?xml version="1.0" encoding="utf-8"?> <account-authenticatorxmlns:android="http://schemas.android.com/apk/res/android"android:accountType="example.com"android:icon="@drawable/ic_launcher"android:smallIcon="@drawable/ic_launcher"android:label="@string/app_name"/>四). 在清單文件中聲明驗證器
在之前的步驟中,你創(chuàng)建了一個捆綁服務,將驗證器和同步適配器框架連接起來。要標識這個服務,你需要再清單文件中添加<service>標簽,將它作為<application>的子標簽:
標簽<intent-filter>配置了一個由android.accounts.AccountAuthenticator的intent所激活的過濾器,這一intent會在系統(tǒng)要運行驗證器時由系統(tǒng)發(fā)出。當過濾器被激活,系統(tǒng)會啟動AuthenticatorService,它是你之前用來封裝認證器的捆綁Service。
<meta-data>標簽聲明了驗證器的元數(shù)據(jù)。android:name屬性將元數(shù)據(jù)和驗證器框架連接起來。android:resource指定了你之前所創(chuàng)建的認證器元數(shù)據(jù)文件的名字。
除了一個認證器,一個同步適配器框架需要一個內容提供器(content provider)。如果你的應用不適用內容提供器,可以閱讀下一節(jié)課程,在下節(jié)課中將會創(chuàng)建一個空的內容提供器;如果你的應用適用的話,可以直接閱讀:Creating a Sync Adapter。
轉載于:https://www.cnblogs.com/jdneo/p/3654420.html
總結
以上是生活随笔為你收集整理的【Android Developers Training】 93. 创建一个空验证器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在visual studio中使用git
- 下一篇: 【转】TCP的SEQ和ACK的生成