Android: read preferences set in authenticator xml

旧城冷巷雨未停 提交于 2019-12-22 04:34:05

问题


I want to read in my code preferences I have set via the authenticator xml file. I found Can't access preferences set in account-authenticator in Android and How can access preferences set in account-authenticator in Android one is completely unanswered and the other says I need to create my own activity. This really sounds odd since that would mean that the preferences I can configure via xml are useless because I never can read them again. That cannot be. Does someone know more about it? If I really have to create an own activity, how would I do this in the case of the authenticator?


回答1:


From the documentation for 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>

So it appears that even though it is possible to put individual preferences in account_preferences.xml it is not intended to be done so the values are not accessible.

See this question and answer for details about how to setup and handle the PreferenceScreen intent.

EDIT

For a very basic working example, you can download the sample app from the Sync Adapter Training Docs and edit as follows.

Create res/xml/account_preferences.xml that looks like this

<?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>

Add android:accountPreferences="@xml/account_preferences" to the account-authenticator tag in authenticator.xml.

This example starts up an existing activity in the example, but could easily start a PreferenceActivity (or any other activity you want). See the Settings guide for details about how to set up the PreferenceActivity.

For a real world example from a core Android app, see the implementation of the Email app here.



来源:https://stackoverflow.com/questions/31029610/android-read-preferences-set-in-authenticator-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!