无法访问在 Android 帐户验证器中设置的首选项 [英] Can't access preferences set in account-authenticator in Android

查看:22
本文介绍了无法访问在 Android 帐户验证器中设置的首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

与 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" />

在 auth_preferences.xml 我有:

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?

推荐答案

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

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.

这篇关于无法访问在 Android 帐户验证器中设置的首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆