2011-12-14 40 views
4

我已經嘗試了很棒的Google示例來同步web服務中的聯繫人,並且工作正常。 這就是所謂的SampleSyncAdapter和真的值得嗎:http://developer.android.com/resources/samples/SampleSyncAdapter/index.html如何添加一個類別到我的SyncAdapter

我贏得成功的一切,但我不能在這個例子中,也沒有在文檔中的方式來添加類別,將鏈接到自定義活動中發現,酷似下面的截圖:

(我只用複選框同步帳戶選項)

enter image description here

所以,我的問題是:我怎麼能添加的帳戶設置類別?

+0

看一看這樣的:http://stackoverflow.com/questions/5486228/how-do-we-control-an-android-sync-adapter-preference它爲我 – kingston 2011-12-15 20:00:11

回答

5

herschel的回答提供了一個link到一個通用的解決方案。下面介紹如何修改SampleSyncAdapter來源補充說,像上面的截圖定製的喜好(安卓2.3.4):

  1. 記住,客戶經理正在作爲一個系統的過程,所以手機將崩潰如果您的代碼中存在未處理的異常,缺少清單條目或xml中存在錯誤。

  2. 創建一個account_preferences.xml資源文件。

    • 實際首選項屏幕的android:key值必須指定爲"account_settings"
    • 如果要將自定義首選項置於某個類別中,則在定義它時需要 關閉PreferenceCategory標記;如果您將PreferenceScreen放在該類別內,則當您單擊首選項時,手機將會崩潰。

    XML:

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
        <PreferenceCategory android:title="General Settings" /> 
        <PreferenceScreen android:key="account_settings" 
          android:title="Account Settings" 
          android:summary="Sync frequency, notifications, etc."> 
         <intent android:action="com.example.android.samplesync.ACCOUNT_SETUP" 
          android:targetPackage="com.example.android.samplesync" 
          android:targetClass="com.example.android.samplesync.AccountPreferences" /> 
        </PreferenceScreen> 
    </PreferenceScreen> 
    
  3. authenticator.xml年底account_preferences.xml添加引用:

    <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" 
        android:accountType="com.example.android.samplesync" android:label="@string/label" 
        android:icon="@drawable/icon" android:smallIcon="@drawable/icon" 
    
        android:accountPreferences="@xml/account_preferences" /> 
    
  4. 創建偏好活動,並將其添加到清單。我從How do we control an Android sync adapter preference?的答案中使用了示例代碼的簡化版本。

    a。 活動添加到清單

    <activity android:label="Account Preferences" android:name=".AccountPreferences" 
        android:theme="@android:style/Theme.Dialog" android:excludeFromRecents="true" /> 
    

    灣這裏是最平凡的AccountPreferences.java

    public class AccountPreferences extends PreferenceActivity { 
        @Override 
        public void onCreate(Bundle icicle) { 
         super.onCreate(icicle); 
         addPreferencesFromResource(R.xml.preferences_resources); 
        } 
    } 
    

    c。下面是preferences_resources.xml使用硬編碼字符串:

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
        <PreferenceCategory android:title="Privacy preferences"/> 
         <CheckBoxPreference android:key="privacy_contacts" android:defaultValue="true" 
           android:summary="Keep contacts private" android:title="Contacts"/> 
        <PreferenceCategory android:title="Outgoing"/> 
         <CheckBoxPreference android:key="allow_mail" android:defaultValue="true" 
           android:summary="Allow email" android:title="Email"/> 
    </PreferenceScreen> 
    
  5. 就是這樣。安裝您的代碼,打開帳戶,然後選擇SampleSyncAdapter帳戶(user1)。選擇帳戶設置,您會看到設置活動。

Custom sync preferences http://i49.tinypic.com/5d6ve0.jpg

相關問題