【问题标题】:How do we control an Android sync adapter preference?我们如何控制 Android 同步适配器首选项?
【发布时间】:2011-07-26 01:36:11
【问题描述】:

In an attempt to write a custom Android sync adapter I followed this。 我成功地在 General setting 中显示了一个条目(帐户设置),并使用上述示例中的以下代码 sn-p。

<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: 从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

我知道这个错误可以通过代码解决。由于“帐户设置”首选项是基于 XML 的代码,因此我遇到了错误。

  1. 谁能帮忙解决这个问题?

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

【问题讨论】:

  • 似乎很多人在 c99 使用这个很棒的教程,但没有很多人走得更远-sigh-

标签: android android-activity fatal-error accountmanager


【解决方案1】:

上面提到的文件/资源​​不在独立包中:我猜这是作者忘记调整的唯一内容:您必须创建自己的首选项类。 这是我的课:

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 项目中的文件。

希望这会有所帮助,祝你好运。

【讨论】:

    【解决方案2】:

    我不会完全回答您的 2 个问题,但我通过以下 3 个步骤解决了这个问题:

    1. 设置帐户首选项 XML
    2. 创建一个活动来管理偏好
    3. 从“偏好编辑”意图中提取帐户信息

    设置帐户首选项 XML

    我使用了一个与 SDK 示例和 c99 Last.fm 应用程序中的非常相似的 account_preferences.xml。考虑以下 sn-p:

    <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 中——这在接收端非常重要,因此您可以知道您正在管理哪个帐户。详情请参阅下文。

    创建偏好管理活动

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

    从“偏好编辑”意图中获取帐户

    当您的 Activity 启动时,您可以从 Extras Bundle 中提取相应的 Account 对象(使用 this.getIntent().getExtras())和键“account”。回想一下,这个 Intent 将是您最初在首选项 XML 文件中指定的那个。 (同样,我找不到这方面的文档,所以通过转储随我的 Intent 传入的 Extras Bundle 的内容找到了它。)一旦你有了帐户,使用 SharedPreferences 加载/保存该帐户的首选项应该很简单,你的数据库,或您喜欢的任何其他方法。

    希望对您有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      相关资源
      最近更新 更多