Android: read preferences set in authenticator xml

715 views Asked by At

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

There are 1 answers

5
mpkuth On BEST ANSWER

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.