我们如何控制Android同步适配器的偏好设置?

7

我试图编写一个自定义的Android同步适配器,我遵循了这个例子。使用上述示例中的以下代码片段后,我成功地在常规设置中显示了一个条目(帐户设置)。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="General Settings" />
        <PreferenceScreen android:key="account_settings"
             android:title="Account Settings"  android:summary="Sync frequency, notifications, etc.">
             <intent android:action="fm.last.android.activity.Preferences.ACCOUNT_SETUP"
                 android:targetPackage="fm.last.android"
                 android:targetClass="fm.last.android.activity.Preferences" />
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

在“常规设置”中,我的代码生成了一个条目(账户设置):

当我点击账户设置时,我会收到以下错误消息,并且设备会不必要地重新启动。

ERROR/AndroidRuntime(30057): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我知道这个错误可以通过代码解决。由于“账户设置”首选项是基于XML的代码,所以我无法解决这个错误。

  1. 有人能帮忙解决这个问题吗?

  2. 我们如何通过代码控制这些偏好设置?


好像很多人在C99上使用这个很棒的教程,但是没有太多人深入学习。-叹气- - olamotte
2个回答

7
我将不会确切回答你的两个问题,但是我通过以下三个步骤来解决这个问题:
  1. 设置账户首选项 XML
  2. 创建一个活动来管理首选项
  3. 从“首选项编辑”意图中提取帐户信息

设置账户首选项 XML

我使用了一个类似于 SDK 示例和 c99 Last.fm 应用程序中的 account_preferences.xml。请考虑以下代码片段:

<PreferenceScreen
          android:key="account_settings"
          android:title="Account Preferences"
          android:summary="Misc account preferences">
          <intent
              android:action="some.unique.action.name.account.EDIT"
              android:targetPackage="com.example.preferences"
              android:targetClass="com.example.preferences.PreferencesActivity">
          </intent>
</PreferenceScreen>

鉴于此,以下是我发现的一些重要点:(请注意,这些都是通过实验发现的,而不是通过任何特定的Android文档 -- 如果这个问题的未来读者有这些参考资料,将其链接在下面会很好。)
  • 此PreferenceScreen的android:key必须为“account_settings”,否则Android无法找到并显示您的偏好设置。
  • 通过使用显式Intent并指定targetPackage和targetClass,Android将直接启动您的Activity,您不需要担心Intent过滤器。
  • Android将当前选定帐户的Account对象存储在此Intent的Extras中--这对于接收端非常重要,以便您可以知道您正在管理哪个帐户。有关更多信息,请参见下文。

创建偏好设置管理活动

接下来,我创建了一个与上述XML中指定的包和类相对应的Activity。请注意,据我所知,Activity的选择取决于您--最常见的是子类化android.preference.PreferenceActivity,但我也直接子类化了Activity。标准Activity开发指南适用于此处...

从"preference editing" Intent中获取帐户

当您的Activity启动时,您可以从Extras Bundle(使用this.getIntent().getExtras())和键“account”中提取相应的Account对象。请记住,这个Intent将是您最初在偏好设置XML文件中指定的Intent。(再次强调,我找不到文档,所以通过转储我的Intent传递的Extras Bundle的内容来找到它。)一旦您拥有了帐户,使用SharedPreferences、数据库或其他任何您喜欢的方法加载/保存该帐户的首选项应该很简单。

希望这可以帮助...


3
上述文件/资源在独立包中不存在:我想这是作者忘记适应的唯一事情。您需要创建自己的首选项类。 以下是我的类:
public class AccountPreferences extends PreferenceActivity {
public static final String TAG = "AccountPreferences";
private boolean shouldForceSync = false;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i(TAG, "onCreate");
    addPreferencesFromResource(R.xml.preferences_resources);

@Override
public void onPause() {
    super.onPause();
    if (shouldForceSync) {
        AccountAuthenticatorService.resyncAccount(this);
    }
}

Preference.OnPreferenceChangeListener syncToggle = new Preference.OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        shouldForceSync = true;
        return true;
    }
};

以下是偏好设置文件:preferences_resources.xml

    <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/privacy_preferences">

    <CheckBoxPreference
        android:key="privacy_contacts"
        android:defaultValue="true"
        android:summary="@string/privacy_contacts_summary" android:title="@string/privacy_contacts_title"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/outgoing_preferences">

    <CheckBoxPreference
        android:key="allow_mail"
        android:defaultValue="true"
        android:summary="@string/allow_mail" android:title="@string/allow_mail_text"/>

</PreferenceCategory>

你需要适应它们,或者更深入地查看他在last.fm项目中的文件。

希望这能帮到你,祝好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接