如何在SyncAdapter中添加类别

Waz*_*_Be 4 settings android android-syncadapter

我尝试了一个伟大的Google示例来同步来自Web服务的联系人,并且工作正常.这称为SampleSyncAdapter,非常值得:http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

我已经完成了所有工作,但我无法在示例中或在文档中找到添加链接到自定义活动的类别的方法,就像下面的屏幕截图一样:

(我只有同步帐户选项和复选框)

在此输入图像描述

所以,我的问题是:如何添加帐户设置类别?

eha*_*ell 5

herschel的回答提供了通用解决方案的链接.以下是如何修改SampleSyncAdapter源以添加自定义首选项(Android 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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. account_preferences.xml在末尾添加引用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" />
    
    Run Code Online (Sandbox Code Playgroud)
  4. 创建首选项活动并将其添加到清单中.我使用了答案中的示例代码的简化版本我们如何控制Android同步适配器首选项?.

    一个.将活动添加到清单:

    <activity android:label="Account Preferences" android:name=".AccountPreferences"
       android:theme="@android:style/Theme.Dialog" android:excludeFromRecents="true" />
    
    Run Code Online (Sandbox Code Playgroud)

    湾 这是最微不足道的AccountPreferences.java:

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

    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>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 而已.安装代码,打开帐户,然后选择SampleSyncAdapter帐户(user1).选择帐户设置,您会看到设置活动.

自定义同步首选项http://i49.tinypic.com/5d6ve0.jpg