如何添加一个类别来我SyncAdapter [英] How can I add a category to my SyncAdapter

查看:270
本文介绍了如何添加一个类别来我SyncAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过了很大的谷歌为例,从同步web服务和做工精细的接触。 这就是所谓的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

我succed的一切,但我不能在这个例子中也没有文件的方式中添加,将链接到一个自定义活动,完全像下面的截图类别:

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)

所以,我的问题是:我怎么能添加帐户设置类别

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

推荐答案

赫歇尔的回答提供链接。以下是如何修改的 SampleSyncAdapter 源添加自定义preferences(安卓2.3.4)看起来像上面的截图:

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. 记住,客户经理正在作为一个系统的过程,所以手机会死机如果有一个在你的code,缺少清单条目,或错误未处理的异常在你的XML。

  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.

创建一个 account_ preferences.xml 资源文件。

  • 在实际的preference屏幕的安卓键值必须指定为account_settings
  • 如果你想要把你的自定义preferences的一类,你需要 关闭 preferenceCategory 标签,当你定义它;如果你把 preferenceScreen 类别里面,当你点击preference手机会死机。
  • 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>

  • 添加引用 account_ preferences.xml authenticator.xml 的结束

  • 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" />
    

  • 创建preference活动,并把它添加到清单。我用的样品code的简化版本,从答案的如何控制一个Android同步适配器preference?

    一个。 添加活动到清单

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

    乙。这里是最微不足道帐户preferences.java

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

    ℃。这里的 preferences_resources.xml 与硬codeD字符串:

    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>
    

  • 就是这样。安装code,打开帐户,并选择您SampleSyncAdapter帐户(用户1 的)。选择的帐户设置的,你看到的设置活动。

  • That's it. Install your code, open the accounts, and select your SampleSyncAdapter account (user1). Select Account Settings and you see the settings activity.

    这篇关于如何添加一个类别来我SyncAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆