无法访问在 Android 帐户验证器中设置的首选项

Mar*_*tyn 5 account android preferences

我正在通过帐户身份验证器在复选框中设置首选项:

Intent settingsIntent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS");
settingsIntent.putExtra("account", mActiveAccount);
startActivityForResult(settingsIntent, ACCOUNT_COMPLETE);
Run Code Online (Sandbox Code Playgroud)

使用以下 xml:

<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" />
Run Code Online (Sandbox Code Playgroud)

在 auth_preferences.xml 我有:

<?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>   
Run Code Online (Sandbox Code Playgroud)

但是当我尝试检索它时,我无法在主代码中访问此复选框首选项:

SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
boolean isChecked = prefs.getBoolean("checkbox_pref", true);
Run Code Online (Sandbox Code Playgroud)

任何人都知道可以从哪里访问基于帐户身份验证器的首选项?

小智 3

在 Activity 中所做的更改基于auth_preferences.xml保存到com.android.settings_preferences.xml并且无法使用应用程序的getSharedPreferences(file, context). 我发现的常见解决方案是触发新的PreferenceActivity帐户首选项。