4

我已经尝试过很棒的 Google 示例来同步来自网络服务的联系人,并且效果很好。这被称为 SampleSyncAdapter,非常值得:http: //developer.android.com/resources/samples/SampleSyncAdapter/index.html

我成功了一切,但我在示例和文档中都找不到添加链接到自定义活动的类别的方法,就像下面的屏幕截图一样:

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

在此处输入图像描述

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

4

1 回答 1

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>
    
  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" />
    
  4. 创建首选项活动并将其添加到清单中。我使用了我们如何控制 Android 同步适配器首选项的答案中的示例代码的简化版本?.

    一个。将活动添加到清单

    <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 )。选择帐户设置,您会看到设置活动。

自定义同步首选项

于 2012-06-01T16:26:47.563 回答