gpt4 book ai didi

android - 如何将类别添加到我的 SyncAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:51 24 4
gpt4 key购买 nike

我已经尝试了很棒的谷歌示例来同步来自网络服务的联系人并且工作正常。这称为 SampleSyncAdapter,非常值得:http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

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

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

enter image description here

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

最佳答案

herschel的答案提供了一个 link到通用解决方案。下面是如何修改 SampleSyncAdapter 添加自定义首选项 (Android 2.3.4) 的源代码,如上图所示:

  1. 请记住,客户经理作为系统进程运行,因此如果您的代码中存在未处理的异常、缺少 list 条目或您的 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. 创建首选项 Activity 并将其添加到 list 中。我使用了 How do we control an Android sync adapter preference? 的答案中示例代码的简化版本。 .

    一个。 将 Activity 添加到 list :

    <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);
    }
    }

    这是带有硬编码字符串的 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)。选择帐户设置,您会看到设置 Activity 。

Custom sync preferences

关于android - 如何将类别添加到我的 SyncAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8506522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com