<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 編寫:[jdneo](https://github.com/jdneo) - 原文:[http://developer.android.com/training/sync-adapters/creating-authenticator.html](http://developer.android.com/training/sync-adapters/creating-authenticator.html) Sync Adapter框架假定你的Sync Adapter在同步數據時,設備存儲端關聯了一個賬戶,且服務器端需要進行登錄驗證。因此,你需要提供一個叫做授權器(Authenticator)的組件作為Sync Adapter的一部分。該組件會集成在Android賬戶及認證框架中,并提供一個標準的接口來處理用戶憑據,比如登錄信息。 如果你的應用不使用賬戶,你仍然需要提供一個授權器組件。在這種情況下,授權器所處理的信息將被忽略,所以你可以提供一個包含了方法存根(Stub Method)的授權器組件。同時你需要提供一個綁定[Service](http://developer.android.com/reference/android/app/Service.html),來允許Sync Adapter框架來調用授權器的方法。 這節課將向你展示如何定義一個能夠滿足Sync Adapter框架要求的Stub授權器。如果你想要提供可以處理用戶賬戶的實際的授權器,可以閱讀:[AbstractAccountAuthenticator](http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html)。 ### 添加一個Stub授權器組件 要在你的應用中添加一個Stub授權器,首先我們需要創建一個繼承[AbstractAccountAuthenticator](http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html)的類,在所有需要重寫的方法中,我們不進行任何處理,僅返回null或者拋出異常。 下面的代碼片段是一個Stub授權器的例子: ~~~ /* * 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(); } } ~~~ ### 將授權器綁定到框架 為了讓Sync Adapter框架可以訪問你的授權器,你必須為它創建一個綁定服務。這一服務提供一個Android Binder對象,允許框架調用你的授權器,并且在授權器和框架間傳遞數據。 因為框架會在它第一次需要訪問授權器時啟動該[Service](http://developer.android.com/reference/android/app/Service.html),你也可以使用該服務來實例化授權器,具體而言,我們需要在服務的[Service.onCreate()](http://developer.android.com/reference/android/app/Service.html#onCreate())方法中調用授權器的構造函數。 下面的代碼樣例展示了如何定義綁定[Service](http://developer.android.com/reference/android/app/Service.html): ~~~ /** * 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(this); } /* * 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(); } } ~~~ ### 添加授權器的元數據(Metadata)文件 若要將你的授權器組件集成到Sync Adapter框架和賬戶框架中,你需要為這些框架提供帶有描述組件信息的元數據。該元數據聲明了你為Sync Adapter創建的賬戶類型以及系統所顯示的UI元素(如果你希望用戶可以看到你創建的賬戶類型)。在你的項目目錄`/res/xml/`下,將元數據聲明于一個XML文件中。你可以自己為該文件按命名,通常我們將它命名為`authenticator.xml`。 在這個XML文件中,包含了一個`<account-authenticator>`標簽,它有下列一些屬性: **android:accountType** Sync Adapter框架要求每一個適配器都有一個域名形式的賬戶類型。框架會將它作為Sync Adapter內部標識的一部分。如果服務端需要登陸,賬戶類型會和賬戶一起發送到服務端作為登錄憑據的一部分。 如果你的服務端不需要登錄,你仍然需要提供一個賬戶類型(該屬性的值用你能控制的一個域名即可)。雖然框架會使用它來管理Sync Adapter,但該屬性的值不會發送到你的服務端。 **android:icon** 指向一個包含圖標的[Drawable](http://developer.android.com/guide/topics/resources/drawable-resource.html)資源。如果你在`res/xml/syncadapter.xml`中通過指定`android:userVisible="true"`讓Sync Adapter可見,那么你必須提供圖標資源。它會在系統的設置中的賬戶(Accounts)這一欄內顯示。 **android:smallIcon** 指向一個包含微小版本圖標的[Drawable](http://developer.android.com/guide/topics/resources/drawable-resource.html)資源。當屏幕尺寸較小時,這一資源可能會替代`android:icon`中所指定的圖標資源。 **android:label** 指明了用戶賬戶類型的本地化String。如果你在`res/xml/syncadapter.xml`中通過指定`android:userVisible="true"`讓Sync Adapter可見,那么你需要提供該String。它會在系統的設置中的賬戶這一欄內顯示,就在你為授權器定義的圖標旁邊。 下面的代碼樣例展示了你之前為授權器創建的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"/> ~~~ ### 在Manifest清單文件中聲明授權器 在之前的步驟中,你已經創建了一個綁定服務,將授權器和Sync Adapter框架連接了起來。為了讓系統可以識別該服務,你需要在清單文件中添加[`<service>`](http://developer.android.com/guide/topics/manifest/service-element.html)標簽,將它作為[`<application>`](http://developer.android.com/guide/topics/manifest/application-element.html)的子標簽: ~~~ <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>`](http://developer.android.com/guide/topics/manifest/intent-filter-element.html)標簽配置了一個可以被`android.accounts.AccountAuthenticator`這一Action所激活的過濾器,這一Intent會在系統要運行授權器時由系統發出。當過濾器被激活后,系統會啟動`AuthenticatorService`,即之前用來封裝授權器的[Service](http://developer.android.com/reference/android/app/Service.html)。 [`<meta-data>`](http://developer.android.com/guide/topics/manifest/meta-data-element.html)標簽聲明了授權器的元數據。[android:name](http://developer.android.com/guide/topics/manifest/meta-data-element.html#nm)屬性將元數據和授權器框架連接起來。[android:resource](http://developer.android.com/guide/topics/manifest/meta-data-element.html#rsrc)指定了你之前所創建的授權器元數據文件的名字。 除了授權器之外,Sync Adapter框架也需要一個Content Provider。如果你的應用并沒有使用Content Provider,可以閱讀下一節課程學習如何創建一個Stub Content Provider;如果你的應用已經使用了ContentProvider,可以直接閱讀:[創建Sync Adapter](#)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看