同步轉換介面架構會假設同步轉換介面會在裝置儲存空間之間轉移資料 該帳戶和伺服器儲存空間相關聯。因此, 架構預期您在同步處理時,會提供「驗證器」元件 轉接頭。這個元件會插入 Android 帳戶和驗證架構 提供處理使用者憑證 (例如登入資訊) 的標準介面。
    即使您的應用程式並未使用帳戶,您仍然需要提供驗證器元件。
    如果您不使用帳戶或伺服器登入,則驗證器所處理的資訊會是
    因此,您可以提供包含虛設常式方法的驗證器元件
    。您還需要提供繫結的 Service
    可讓同步處理轉換介面架構呼叫驗證器的方法。
    本課程將說明如何定義虛設常式驗證器的各個部分
    符合同步處理轉換介面架構的要求。如果您需要提供
    處理使用者帳戶的驗證器,請閱讀
    AbstractAccountAuthenticator。
新增虛設驗證器元件
    如要為應用程式新增虛設常式驗證器元件,請建立可擴充
    AbstractAccountAuthenticator,然後找到必要方法
    方法包括傳回 null 或擲回例外狀況
以下程式碼片段為虛設常式驗證器類別範例:
Kotlin
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ class Authenticator(context: Context) // Simple constructor : AbstractAccountAuthenticator(context) { // Editing properties is not supported override fun editProperties(r: AccountAuthenticatorResponse, s: String): Bundle { throw UnsupportedOperationException() } // Don't add additional accounts @Throws(NetworkErrorException::class) override fun addAccount( r: AccountAuthenticatorResponse, s: String, s2: String, strings: Array<String>, bundle: Bundle ): Bundle? = null // Ignore attempts to confirm credentials @Throws(NetworkErrorException::class) override fun confirmCredentials( r: AccountAuthenticatorResponse, account: Account, bundle: Bundle ): Bundle? = null // Getting an authentication token is not supported @Throws(NetworkErrorException::class) override fun getAuthToken( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Getting a label for the auth token is not supported override fun getAuthTokenLabel(s: String): String { throw UnsupportedOperationException() } // Updating user credentials is not supported @Throws(NetworkErrorException::class) override fun updateCredentials( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Checking features for the account is not supported @Throws(NetworkErrorException::class) override fun hasFeatures( r: AccountAuthenticatorResponse, account: Account, strings: Array<String> ): Bundle { throw UnsupportedOperationException() } }
Java
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ public class Authenticator extends AbstractAccountAuthenticator { // Simple constructor public Authenticator(Context context) { super(context); } // Editing properties is not supported @Override public Bundle editProperties( AccountAuthenticatorResponse r, String s) { throw new UnsupportedOperationException(); } // Don't add additional accounts @Override public Bundle addAccount( AccountAuthenticatorResponse r, String s, String s2, String[] strings, Bundle bundle) throws NetworkErrorException { return null; } // Ignore attempts to confirm credentials @Override public Bundle confirmCredentials( AccountAuthenticatorResponse r, Account account, Bundle bundle) throws NetworkErrorException { return null; } // Getting an authentication token is not supported @Override public 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 @Override public String getAuthTokenLabel(String s) { throw new UnsupportedOperationException(); } // Updating user credentials is not supported @Override public Bundle updateCredentials( AccountAuthenticatorResponse r, Account account, String s, Bundle bundle) throws NetworkErrorException { throw new UnsupportedOperationException(); } // Checking features for the account is not supported @Override public Bundle hasFeatures( AccountAuthenticatorResponse r, Account account, String[] strings) throws NetworkErrorException { throw new UnsupportedOperationException(); } }
將驗證器繫結至架構
為了讓同步處理轉換介面架構存取驗證器,您必須建立繫結 這些服務這項服務提供 Android 繫結器物件,讓架構 呼叫驗證器,並在驗證器和架構之間傳遞資料。
    下列程式碼片段說明如何定義繫結的 Service:
Kotlin
/** * A bound Service that instantiates the authenticator * when started. */ class AuthenticatorService : Service() { // Instance field that stores the authenticator object private lateinit var mAuthenticator: Authenticator override fun onCreate() { // Create a new authenticator object mAuthenticator = Authenticator(getApplicationContext()) } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ override fun onBind(intent: Intent?): IBinder = mAuthenticator.iBinder }
Java
/** * A bound Service that instantiates the authenticator * when started. */ public class AuthenticatorService extends Service { ... // Instance field that stores the authenticator object private Authenticator mAuthenticator; @Override public void onCreate() { // Create a new authenticator object mAuthenticator = new Authenticator(getApplicationContext()); } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ @Override public IBinder onBind(Intent intent) { return mAuthenticator.getIBinder(); } }
新增驗證器中繼資料檔案
    如要將驗證器元件插入同步轉換介面和帳戶架構,您需要:
    為這些架構提供描述元件的中繼資料此中繼資料宣告
    您為同步轉接程式建立的帳戶類型,並宣告使用者介面元素
    是否要讓使用者看見帳戶類型。聲明此項目
    儲存在應用程式專案 /res/xml/ 目錄中的 XML 檔案中的中繼資料。
    您可以為檔案指定任何名稱,但通常名稱通常為 authenticator.xml。
    這個 XML 檔案包含 <account-authenticator> 元素,
    包含下列屬性:
- 
        android:accountType
- 
        同步轉換介面架構要求每個同步轉換介面都必須擁有帳戶類型,格式如下:
        網域名稱該架構會將帳戶類型做為同步處理轉換轉接工具的一部分
        進行內部識別。如果是需要登入的伺服器,請選擇帳戶類型及
        把使用者帳戶當做登入憑證的一部分傳送至伺服器。
    如果伺服器不需要登入,您仍然必須提供帳戶類型。對於 值,請使用您控管的網域名稱。架構會使用 同步處理轉接器,該值就不會傳送至您的伺服器。 
- 
        android:icon
- 
        指向可繪項目的指標
        包含圖示的資源。如果您要顯示同步轉換介面,請指定
        res/xml/syncadapter.xml中的android:userVisible="true"屬性, 您必須提供此圖示資源該帳戶位於以下帳戶的「帳戶」部分: 系統的「設定」應用程式。
- 
        android:smallIcon
- 
        指向可繪項目的指標
        包含小型圖示的資源系統可能會使用這項資源
        android:icon(位於系統「設定」應用程式專區中的帳戶) 中, 根據螢幕大小而定
- 
        android:label
- 
        可本地化的字串,用來識別使用者的帳戶類型。如果你使用同步轉換介面
        只要在android:userVisible="true"res/xml/syncadapter.xml,那麼您應該提供這個字串。它會顯示在 系統「設定」應用程式的「帳戶」部分,在您為 驗證器。
下列程式碼片段顯示您先前建立的驗證器的 XML 檔案:
<?xml version="1.0" encoding="utf-8"?> <account-authenticator xmlns: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"/>
在資訊清單中宣告驗證器
    在上一個步驟中,您建立了繫結 Service,用來連結驗證器
    加入同步處理轉換介面架構若要向系統識別此服務,請在應用程式中宣告
    將下列資訊提供給開發人員
    <service>
    做為
<application>:
<service android:name="com.example.android.syncadapter.AuthenticatorService"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" /> </service>
    
<intent-filter>
    元素會設定由意圖動作觸發的篩選器
    android.accounts.AccountAuthenticator,讓系統傳送這些程式來執行
    驗證器。篩選器觸發時,系統會啟動 AuthenticatorService、
    您提供給驗證器的繫結 Service。
    
<meta-data>
    元素會宣告驗證器的中繼資料。
android:name
    屬性會將中繼資料連結至驗證架構。
android:resource
    元素會指定您之前建立的驗證器中繼資料檔案名稱。
除了驗證器以外,同步轉換介面也需要內容供應器。如果應用程式 已經有內容供應器了,請前往下一個課程,瞭解如何製作虛設常式內容 供應商;否則,請參閱「建立同步轉換介面」課程。
