【问题标题】:How can I add a category to my SyncAdapter如何将类别添加到我的 SyncAdapter
【发布时间】:2012-01-20 08:21:10
【问题描述】:
【问题讨论】:
标签:
android
settings
android-syncadapter
【解决方案1】:
herschel 的回答为通用解决方案提供了link。以下是如何修改 SampleSyncAdapter 源以添加类似于上面屏幕截图的自定义首选项(Android 2.3.4):
请记住,客户经理作为系统进程运行,因此如果您的代码中存在未处理的异常、缺少清单条目或 xml 中的错误,手机将崩溃 .
-
创建一个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>
-
在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" />
-
创建一个偏好活动并将其添加到清单中。我使用了来自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>
就是这样。安装您的代码,打开帐户,然后选择您的 SampleSyncAdapter 帐户 (user1)。选择帐户设置,您会看到设置活动。