Can't access preferences set in account-authenticator in Android
Asked Answered
E

1

6

I'm settings a preference in a checkbox via an account-authenticator:

Intent settingsIntent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS");
settingsIntent.putExtra("account", mActiveAccount);
startActivityForResult(settingsIntent, ACCOUNT_COMPLETE);

with the xml of:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.example.auth"
android:label="@string/auth_label"
android:accountPreferences="@xml/auth_preferences" />

and in auth_preferences.xml I have:

<?xml version="1.0" encoding="UTF-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/auth_preferences_general_group" />
<PreferenceScreen android:key="account_settings"
    android:title="@string/auth_preferences_general_details_title" 
    android:summary="@string/auth_preferences_general_details_description">
    <intent android:action="com.example.ACCOUNT_SETUP"
        android:targetPackage="com.example.core"
        android:targetClass="com.example.authentication.AuthenticatorAccountOptions" />
</PreferenceScreen>
<PreferenceCategory android:title="@string/auth_preferences_data_sync_group" >
    <CheckBoxPreference
          android:key="checkbox_pref"
          android:title="@string/auth_preferences_data_sync_syncwidget_title" 
          android:summary="@string/auth_preferences_data_sync_syncwidget_description"
          android:defaultValue="true"
          android:persistent="true" />  
</PreferenceCategory>   

But I'm unable to access this checkbox preferences in the main code when I try and retrieve it :

SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
boolean isChecked = prefs.getBoolean("checkbox_pref", true);

Anyone know where the account-authenticator based preferences can be accessed from?

Emmett answered 4/5, 2011 at 14:56 Comment(0)
C
3

The changes made in the Activity based on auth_preferences.xml are saved to com.android.settings_preferences.xml and it can't be accessed using your application's getSharedPreferences(file, context). Common solution I found is to trigger a new PreferenceActivity in account preferences instead.

Cotterell answered 1/1, 2012 at 22:17 Comment(2)
How would you do this? And why is there the possibility to create preferences if you cannot read them?Crossstitch
This is not only the common solution but also what is intended 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."Rack

© 2022 - 2024 — McMap. All rights reserved.