【问题标题】:How can I add a category to my SyncAdapter如何将类别添加到我的 SyncAdapter
【发布时间】:2012-01-20 08:21:10
【问题描述】:

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

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

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

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

【问题讨论】:

标签: android settings android-syncadapter


【解决方案1】:

herschel 的回答为通用解决方案提供了link。以下是如何修改 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. 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? 答案的示例代码的简化版本。

    一个。 将活动添加到清单中

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

    b.这里是最琐碎的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)。选择帐户设置,您会看到设置活动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多