亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【Android Developers Training】 93. 創建一個

系統 1851 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。

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


同步適配器框架假定你的同步適配器在同步數據時,設備存儲會有一個賬戶,服務器存儲端會有登錄驗證。因此,框架期望你提供一個叫做驗證器的組件作為你的同步適配器的一部分。該組件會植入Android賬戶及認證框架,并提供一個標準的接口來處理用戶憑據,比如登錄信息。

甚至,如果你的應用不使用賬戶,你仍然需要提供一個認證器組件。如果你不使用賬戶或者服務器登錄,認證器所處理的信息將被忽略,所以你可以提供一個認證器組件,它包括了一個空的實現。同時你需要提供一個捆綁的 Service ,來允許同步適配器框架來調用認證器的方法。

這節課將向你展示如何定義一個空驗證器的所有滿足其實現要求的部件。如果你想要提供一個真實的處理用戶賬戶的驗證器,可以閱讀: AbstractAccountAuthenticator


一). 添加一個空驗證期組件

要在你的應用中添加一個空驗證器,創建一個繼承 AbstractAccountAuthenticator 的類,并將要覆寫的方法置空(這樣就不會做任何處理了),返回null或者拋出異常。

下面的代碼片段是一個空驗證器的例子:

      
        /*
      
      
        

 * 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? binder對象,允許框架調用你的驗證器,并且在驗證器和框架間傳輸數據。

因為框架會在它需要第一次訪問驗證器時啟動 Service ,你也可以使用服務來實例化驗證器,方法是通過在服務的 Service.onCreate() 方法中調用驗證器的構造函數。

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

      
        /**
      
      
        

 * 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();

    }

}
      
    

三). 添加驗證器的元數據文件

要將你的驗證器組件插入到同步適配器和賬戶框架中,你需要為框架提供帶有描述組件的元數據。該元數據聲明了你創建的同步適配器的賬戶類型以及系統所顯示的用戶接口元素(如果你希望將你的賬戶類型對用戶可見)。在你的項目目錄:“ /res/xml/ ”下,將元數據聲明于一個XML文件中。你可以隨便為它起一個名字,一般來說,可以叫“ authenticator.xml

在這個XML文件中,包含單個元素 <account-authenticator> ,它有下列一些屬性:

android:accountType

同步適配器框架需要每一個適配器以域名的形式擁有一個賬戶類型。框架使用作為其內部的標識。對于需要登錄的服務器,賬戶類型會和賬戶一起發送到服務端作為登錄憑據的一部分。

如果你的服務不需要登錄,你仍然需要提供一個賬戶類型。值的話就用你能控制的一個域名即可。由于框架會使用它來管理同步適配器,所以值不會發送到服務器上。

android:icon

指向一個包含一個圖標的 Drawable 資源的指針。如果你在“ res/xml/syncadapter.xml ”中通過指定“ android:userVisible="true" ”讓同步適配器可見,那么你必須提供圖標資源。它會在系統的設置中的賬戶這一欄內顯示。

android:smallIcon

指向一個包含一個微小版本圖標的 Drawable 資源的指針。結合具體的屏幕大小,這一資源可能會替代“ android:icon ”中所指定的圖標資源。

android:label

將指明了用戶賬戶類型的string本地化。 如果你在“ res/xml/syncadapter.xml ”中通過指定“ android:userVisible="true" ”讓同步適配器可見,那么你需要提供這個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"
      
      
        />
      
    

四). 在清單文件中聲明驗證器

在之前的步驟中,你創建了一個捆綁服務,將驗證器和同步適配器框架連接起來。要標識這個服務,你需要再清單文件中添加 <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的intent所激活的過濾器,這一intent會在系統要運行驗證器時由系統發出。當過濾器被激活,系統會啟動 AuthenticatorService ,它是你之前用來封裝認證器的捆綁 Service

<meta-data> 標簽聲明了驗證器的元數據。 android:name 屬性將元數據和驗證器框架連接起來。 android:resource 指定了你之前所創建的認證器元數據文件的名字。

除了一個認證器,一個同步適配器框架需要一個內容提供器(content provider)。如果你的應用不適用內容提供器,可以閱讀下一節課程,在下節課中將會創建一個空的內容提供器;如果你的應用適用的話,可以直接閱讀: Creating a Sync Adapter

【Android Developers Training】 93. 創建一個空驗證器


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91年精品国产福利线观看久久 | 亚洲国产精品成 | 四虎影剧院| 成人免费网站视频www | 亚洲精品欧美精品 | 亚州色吧 | 亚洲精品亚洲人成毛片不卡 | 97在线视频免费播放 | 久久久精品视频免费观看 | 操一操 | 国产国语videosex | 99re热这里只有精品视频 | 日韩欧美一区二区久久黑人 | 婷婷激情在线视频 | 伊人成年综合网 | 国产在线看不卡一区二区 | 久久精品亚洲综合一品 | 天天撸天天操 | 国产欧美一区二区三区视频 | 久久亚洲国产精品一区二区 | 九九热视频这里只有精品 | 亚洲国产精品久久婷婷 | 天天做天天爱天天怼 | 视频一区在线 | 国产精品久久久久天天影视 | 午夜在线影院 | 久久午夜夜伦伦鲁鲁片 | 青青草国产三级精品三级 | 国产99视频精品一区 | 日韩欧美国产一区二区三区 | 国产日产欧美精品 | 欧美黄色a | 久久国产乱子伦精品免费强 | 亚洲综合香蕉 | 久草在线在线视频 | 久久精品道一区二区三区 | 亚洲 欧美 自拍 卡通 综合 | 中文字幕免费在线视频 | h片在线播放免费高清 | 午夜探花 | 久久天天丁香婷婷中文字幕 |