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

发布于 2024-11-05 01:46:45 字数 1892 浏览 6 评论 0原文

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

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 中我有:

<?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>   

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2024-11-12 01:46:45

Activity 中基于 auth_preferences.xml 所做的更改将保存到 com.android.settings_preferences.xml,并且无法使用应用程序的 getSharedPreferences(文件,上下文)。我发现的常见解决方案是在帐户首选项中触发新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文