Android:读取身份验证器中设置的首选项 xml

Android: read preferences set in authenticator xml

我想阅读我通过身份验证器 xml 文件设置的代码首选项。我发现 Can't access preferences set in account-authenticator in Android and How can access preferences set in account-authenticator in Android 一个完全没有答案,另一个说我需要创建自己的 activity。这听起来真的很奇怪,因为这意味着我可以通过 xml 配置的首选项是无用的,因为我永远无法再次阅读它们。那不可能。有人知道更多吗?如果我真的必须创建一个自己的 activity,在身份验证器的情况下我将如何做?

来自 AbstractAccountAuthenticator 的文档:

The preferences attribute points to a PreferenceScreen xml hierarchy that contains a list of PreferenceScreens that can be invoked to manage the authenticator. An example is:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="@string/title_fmt"/>
    <PreferenceScreen
        android:key="key1"
        android:title="@string/key1_action"
        android:summary="@string/key1_summary">
        <intent
            android:action="key1.ACTION"
            android:targetPackage="key1.package"
            android:targetClass="key1.class"/>
    </PreferenceScreen>
</PreferenceScreen>

所以看起来即使可以将个人首选项放入 account_preferences.xml 中,但并不打算这样做,因此无法访问这些值。

有关如何设置和处理 PreferenceScreen 意图的详细信息,请参阅 this 问答。

编辑

对于一个非常基本的工作示例,您可以从 Sync Adapter Training Docs 下载示例应用程序并进行如下编辑。

创建 res/xml/account_preferences.xml 看起来像这样

<?xml version="1.0" encoding="UTF-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Category"/>
    <PreferenceScreen
        android:key="key1"
        android:title="@string/app_name"
        android:summary="@string/account_name">
        <intent
            android:targetPackage="com.example.android.network.sync.basicsyncadapter"
            android:targetClass="com.example.android.network.sync.basicsyncadapter.EntryListActivity"/>
    </PreferenceScreen>
</PreferenceScreen>

android:accountPreferences="@xml/account_preferences"添加到authenticator.xml中的account-authenticator标签。

此示例启动示例中现有的 activity,但可以轻松启动 PreferenceActivity(或您想要的任何其他 activity)。有关如何设置 PreferenceActivity 的详细信息,请参阅 Settings 指南。

有关核心 Android 应用程序的真实示例,请参阅电子邮件应用程序 here 的实现。