Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何在SyncAdapter中添加类别大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试了一个伟大的Google示例来同步来自Web服务的联系人,并且工作正常.
这称为SampleSyncAdapter,非常值得: http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

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

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

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

解决方法

herschel的答案为通用解决方案提供了 link.以下是如何修改 SampleSyncAdapter源以添加自定义首选项(Android 2.3.4),如上图所示:

>请记住,帐户管理器作为系统进程运行,因此如果代码中存在未处理的异常,缺少清单条目或xml中的错误,手机将崩溃.
>创建account_preferences.xml资源文件.

>实际首选项屏幕的android:键值必须指定为“account_setTings”.
>如果要将自定义首选项放在类别中,则需要
在定义时关闭PreferenceCategory标记;如果您将PreferenceScreen放在类别中,则单击首选项时手机将崩溃.

XML:

<PreferenceScreen xmlns:android="http://scheR_306_11845@as.android.com/apk/res/android">
    <PreferenceCategory android@R_998_6964@="General SetTings" />
    <PreferenceScreen android:key="account_setTings"
            android@R_998_6964@="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://scheR_306_11845@as.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" />

湾这是最简单的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://scheR_306_11845@as.android.com/apk/res/android">
    <PreferenceCategory android@R_998_6964@="Privacy preferences"/>
        <checkBoxPreference android:key="privacy_contacts" android:DefaultValue="true"
                android:sumMary="Keep contacts private" android@R_998_6964@="Contacts"/>
    <PreferenceCategory android@R_998_6964@="Outgoing"/>
        <checkBoxPreference android:key="allow_mail" android:DefaultValue="true"
                android:sumMary="Allow email" android@R_998_6964@="Email"/>
</PreferenceScreen>

>就是这样.安装代码,打开帐户,然后选择SampleSyncAdapter帐户(user1).选择帐户设置,您会看到设置活动.

Custom sync preferences http://i49.tinypic.com/5d6ve0.jpg

大佬总结

以上是大佬教程为你收集整理的android – 如何在SyncAdapter中添加类别全部内容,希望文章能够帮你解决android – 如何在SyncAdapter中添加类别所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。