开发者

Can't access preferences set in account-authenticator in Android

开发者 https://www.devze.com 2023-03-02 15:32 出处:网络
I\'m settings a preference in a checkbox via an account-authenticator: Intent settingsIntent = new Intent(\"android.settings.ACCOUNT_SYNC_SETTINGS\");

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" 
          a开发者_C百科ndroid: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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消