ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何在SyncAdapter中添加类别

2019-06-21 05:09:59  阅读:188  来源: 互联网

标签:android settings android-syncadapter


我尝试了一个伟大的Google示例来同步来自Web服务的联系人,并且工作正常.
这称为SampleSyncAdapter,非常值得:http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

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

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

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

解决方法:

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

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

>实际首选项屏幕的android:键值必须指定为“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" />

湾这是最简单的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).选择帐户设置,您会看到设置活动.

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

标签:android,settings,android-syncadapter
来源: https://codeday.me/bug/20190621/1251942.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有