如何向我的 SyncAdapter 添加类别

发布于 2024-12-20 23:56:37 字数 447 浏览 2 评论 0原文

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

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

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

在此处输入图像描述

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

I have tried the great Google example to sync contact from a webservice and that work fine.
This is called the SampleSyncAdapter and really worth it: http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

I succed everything, but I cannot found in the example nor in the documentation a way to add a category that would link to a custom activity, exactly like the screenshot below:

(I have only the sync account option with the checkbox)

enter image description here

So, my question is: how can I add the account settings category?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-12-27 23:56:38

herschel 的回答提供了链接到通用解决方案。以下是如何将 SampleSyncAdapter 源修改为添加自定义首选项 (Android 2.3.4),如上面的屏幕截图所示:

  1. 请记住,帐户管理器是作为系统进程运行的,因此如果您的帐户中存在未处理的异常,手机将会崩溃代码、清单条目丢失或 xml 中存在错误。

  2. 创建 account_preferences.xml 资源文件。

    • 实际首选项屏幕的 android:key 值必须指定为 “account_settings”
    • 如果您想将自定义偏好设置放入某个类别中,则需要
      定义时关闭 PreferenceCategory 标记;如果您将 PreferenceScreen 放入类别中,则当您单击首选项时手机将会崩溃。

    XML:

    
        
        
            
        
    
    
  3. authenticator.xml 末尾添加对 account_preferences.xml 的引用:

    
    
  4. 创建首选项活动并将其添加到清单中。我使用了 我们如何控制 Android 同步适配器首选项?

    a. 将活动添加到清单

    
    

    b.这是最简单的 AccountPreferences.java

    public class AccountPreferences 扩展 PreferenceActivity {
        @覆盖
        公共无效onCreate(捆绑冰柱){
            super.onCreate(冰柱);
            addPreferencesFromResource(R.xml.preferences_resources);
        }
    }
    

    c.这是带有硬编码字符串的 preferences_resources.xml

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

自定义同步偏好设置

herschel's answer provides a link to a generic solution. Here's how to modify the SampleSyncAdapter source to add custom preferences (Android 2.3.4) that look like the screenshot above:

  1. Remember that the account manager is running as a system process, so the phone will crash if there's an unhandled exception in your code, a missing manifest entry, or an error in your xml.

  2. Create an account_preferences.xml resource file.

    • The actual preference screen's android:key value must be specified as "account_settings".
    • If you want to put your custom preferences in a category, you need to
      close the PreferenceCategory tag when you define it; if you put the PreferenceScreen inside the category the phone will crash when you click on the preference.

    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. Add a reference to account_preferences.xml at the end of authenticator.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. Create a preference activity and add it to the manifest. I used a simplified version of the sample code from the answer to How do we control an Android sync adapter preference?.

    a. Add the activity to the manifest:

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

    b. Here's the most trivial AccountPreferences.java:

    public class AccountPreferences extends PreferenceActivity {
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            addPreferencesFromResource(R.xml.preferences_resources);
        }
    }
    

    c. Here's preferences_resources.xml with hard-coded strings:

    <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. That's it. Install your code, open the accounts, and select your SampleSyncAdapter account (user1). Select Account Settings and you see the settings activity.

Custom sync preferences

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文