創(chuàng)建 Stub 授權器

原文:http://developer.android.com/training/sync-adapters/creating-authenticator.html

Sync Adapter 框架假定我們的 Sync Adapter 在同步數(shù)據(jù)時,設備存儲端關聯(lián)了一個賬戶,且服務器端需要進行登錄驗證。因此,我們需要提供一個叫做授權器(Authenticator)的組件作為 Sync Adapter 的一部分。該組件會集成在 Android 賬戶及認證框架中,并提供一個標準的接口來處理用戶憑據(jù),比如登錄信息。

即使我們的應用不使用賬戶,我們仍然需要提供一個授權器組件。在這種情況下,授權器所處理的信息將被忽略,所以我們可以提供一個包含了方法存根(Stub Method)的授權器組件。同時我們需要提供一個綁定Service,來允許 Sync Adapter 框架調用授權器的方法。

這節(jié)課將展示如何定義一個能夠滿足 Sync Adapter 框架要求的 Stub 授權器。如果我們想要提供可以處理用戶賬戶的實際的授權器,可以閱讀:AbstractAccountAuthenticator

添加一個 Stub 授權器組件

要在應用中添加一個 Stub 授權器,首先我們需要創(chuàng)建一個繼承AbstractAccountAuthenticator的類,在所有需要重寫的方法中,我們不進行任何處理,僅返回 null 或者拋出異常。

下面的代碼片段是一個 Stub 授權器的例子:

/*

* Implement AbstractAccountAuthenticator and stub out all

* of its methods

*/publicclassAuthenticatorextendsAbstractAccountAuthenticator{// Simple constructorpublicAuthenticator(Context context){super(context);? ? }// Editing properties is not supported@OverridepublicBundleeditProperties(

AccountAuthenticatorResponse r, String s){thrownewUnsupportedOperationException();? ? }// Don't add additional accounts@OverridepublicBundleaddAccount(

AccountAuthenticatorResponse r,

String s,

String s2,

String[] strings,

Bundle bundle)throwsNetworkErrorException{returnnull;? ? }// Ignore attempts to confirm credentials@OverridepublicBundleconfirmCredentials(

AccountAuthenticatorResponse r,

Account account,

Bundle bundle)throwsNetworkErrorException{returnnull;? ? }// Getting an authentication token is not supported@OverridepublicBundlegetAuthToken(

AccountAuthenticatorResponse r,

Account account,

String s,

Bundle bundle)throwsNetworkErrorException{thrownewUnsupportedOperationException();? ? }// Getting a label for the auth token is not supported@OverridepublicStringgetAuthTokenLabel(String s){thrownewUnsupportedOperationException();? ? }// Updating user credentials is not supported@OverridepublicBundleupdateCredentials(

AccountAuthenticatorResponse r,

Account account,

String s, Bundle bundle)throwsNetworkErrorException{thrownewUnsupportedOperationException();? ? }// Checking features for the account is not supported@OverridepublicBundlehasFeatures(

AccountAuthenticatorResponse r,

Account account, String[] strings)throwsNetworkErrorException{thrownewUnsupportedOperationException();? ? }}

將授權器綁定到框架

為了讓 Sync Adapter 框架可以訪問我們的授權器,我們必須為它創(chuàng)建一個綁定服務。這一服務提供一個 Android Binder 對象,允許框架調用我們的授權器,并且在授權器和框架間傳遞數(shù)據(jù)。

因為框架會在它第一次需要訪問授權器時啟動該Service,所以我們也可以使用該服務來實例化授權器。具體而言,我們需要在服務的Service.onCreate()方法中調用授權器的構造函數(shù)。

下面的代碼樣例展示了如何定義綁定Service

/**

* A bound Service that instantiates the authenticator

* when started.

*/publicclassAuthenticatorServiceextendsService{? ? ...// Instance field that stores the authenticator objectprivateAuthenticator mAuthenticator;@OverridepublicvoidonCreate(){// Create a new authenticator objectmAuthenticator =newAuthenticator(this);? ? }/*

* When the system binds to this Service to make the RPC call

* return the authenticator's IBinder.

*/@OverridepublicIBinderonBind(Intent intent){returnmAuthenticator.getIBinder();? ? }}

添加授權器的元數(shù)據(jù)(Metadata)文件

若要將我們的授權器組件集成到 Sync Adapter 框架和賬戶框架中,我們需要為這些框架提供帶有描述組件信息的元數(shù)據(jù)。該元數(shù)據(jù)聲明了我們?yōu)?Sync Adapter 創(chuàng)建的賬戶類型以及系統(tǒng)所顯示的 UI 元素(如果希望用戶可以看到我們創(chuàng)建的賬戶類型)。在我們的項目目錄/res/xml/下,將元數(shù)據(jù)聲明于一個 XML 文件中。我們可以自己為該文件按命名,通常我們將它命名為authenticator.xml。

在這個 XML 文件中,包含了一個標簽,它有下列一些屬性:

android:accountType

Sync Adapter 框架要求每一個適配器都有一個域名形式的賬戶類型。框架會將它作為 Sync Adapter 內部標識的一部分。如果服務端需要登陸,賬戶類型會和賬戶一起發(fā)送到服務端作為登錄憑據(jù)的一部分。

如果我們的服務端不需要登錄,我們仍然需要提供一個賬戶類型(該屬性的值用我們能控制的一個域名即可)。雖然框架會使用它來管理 Sync Adapter,但該屬性的值不會發(fā)送到服務端。

android:icon

指向一個包含圖標的Drawable資源。如果我們在res/xml/syncadapter.xml中通過指定android:userVisible="true"讓 Sync Adapter 可見,那么我們必須提供圖標資源。它會在系統(tǒng)的設置中的賬戶(Accounts)這一欄內顯示。

android:smallIcon

指向一個包含微小版本圖標的Drawable資源。當屏幕尺寸較小時,這一資源可能會替代android:icon中所指定的圖標資源。

android:label

指明了用戶賬戶類型的本地化字符串。如果我們在res/xml/syncadapter.xml中通過指定android:userVisible="true"讓 Sync Adapter 可見,那么我們需要提供該字符串。它會在系統(tǒng)的設置中的賬戶這一欄內顯示,就在我們?yōu)槭跈嗥鞫x的圖標旁邊。

下面的代碼樣例展示了我們之前為授權器創(chuàng)建的 XML 文件:

在 Manifest 文件中聲明授權器

在之前的步驟中,我們已經創(chuàng)建了一個綁定服務,將授權器和 Sync Adapter 框架連接了起來。為了讓系統(tǒng)可以識別該服務,我們需要在 Manifest 文件中添加標簽,將它作為的子標簽:

標簽配置了一個可以被android.accounts.AccountAuthenticator這一 Action 所激活的過濾器,這一 Intent 會在系統(tǒng)要運行授權器時由系統(tǒng)發(fā)出。當過濾器被激活后,系統(tǒng)會啟動AuthenticatorService,即之前用來封裝授權器的Service

標簽聲明了授權器的元數(shù)據(jù)。android:name屬性將元數(shù)據(jù)和授權器框架連接起來。android:resource指定了我們之前所創(chuàng)建的授權器元數(shù)據(jù)文件的名字。

除了授權器之外,Sync Adapter 框架也需要一個 Content Provider。如果我們的應用并沒有使用 Content Provider,那么可以閱讀下一節(jié)課程學習如何創(chuàng)建一個 Stub Content Provider;如果我們的應用已經使用了 ContentProvider,可以直接閱讀:創(chuàng)建 Sync Adapter

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 原文:http://developer.android.com/training/sync-adapters/cr...
    tiger桂閱讀 490評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,076評論 25 708
  • 原文:http://developer.android.com/training/sync-adapters/ru...
    tiger桂閱讀 526評論 0 2
  • 我們身處的這個世界,誰也不能完全的開研究透,地球是,宇宙是,甚至連我們自己都不能百分百的說了解 先說我們居住的宇宙...
    山河識淺談閱讀 339評論 0 1
  • 脖子伸長 如菜市口的看客 一刀見血 殷紅如河 悲傷的對白 誰還記得 散場的意猶未盡 幸災樂禍甚多 口口相傳 道聽途...
    飛狐119閱讀 176評論 0 2