How can I add a category to my SyncAdapter

2019-04-10 03:19发布

问题:

I have tried the great Google example to sync contact from a webservice and that work fine. This is called the SampleSyncAdapter and really worth it: http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

I succed everything, but I cannot found in the example nor in the documentation a way to add a category that would link to a custom activity, exactly like the screenshot below:

(I have only the sync account option with the checkbox)

So, my question is: how can I add the account settings category?

回答1:

herschel's answer provides a link to a generic solution. Here's how to modify the SampleSyncAdapter source to add custom preferences (Android 2.3.4) that look like the screenshot above:

  1. Remember that the account manager is running as a system process, so the phone will crash if there's an unhandled exception in your code, a missing manifest entry, or an error in your xml.

  2. Create an account_preferences.xml resource file.

    • The actual preference screen's android:key value must be specified as "account_settings".
    • If you want to put your custom preferences in a category, you need to close the PreferenceCategory tag when you define it; if you put the PreferenceScreen inside the category the phone will crash when you click on the preference.

    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. Add a reference to account_preferences.xml at the end of authenticator.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. Create a preference activity and add it to the manifest. I used a simplified version of the sample code from the answer to How do we control an Android sync adapter preference?.

    a. Add the activity to the manifest:

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

    b. Here's the most trivial AccountPreferences.java:

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

    c. Here's preferences_resources.xml with hard-coded strings:

    <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. That's it. Install your code, open the accounts, and select your SampleSyncAdapter account (user1). Select Account Settings and you see the settings activity.

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