android - 我们如何控制 Android 同步适配器首选项?

标签 android android-activity fatal-error accountmanager

In an attempt to write a custom Android sync adapter I followed this . 我成功地使用上述示例中的以下代码 fragment 在常规设置中显示了一个条目(帐户设置)。

<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. 我们如何通过代码控制这些偏好?

最佳答案

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

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

设置帐户首选项 XML

我使用的 account_preferences.xml 与 SDK 示例和 c99 Last.fm 应用程序中的非常相似。考虑以下代码段:

<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 文档发现这些的——如果这个问题的 future 读者有这些引用资料,它会很高兴将它们链接起来。)

  • 此 PreferenceScreen 的 android:key 必须为“account_settings”,否则 Android 将找不到并显示您的首选项
  • 通过使用显式 Intent 并指定 targetPackage 和 targetClass,android 将直接启动您的 Activity,您无需担心 Intent 过滤器。
  • Android 将当前所选帐户的帐户对象存储在此 Intent 的 Extras 中——这对接收端非常重要,因此您可以知道您管理的是哪个帐户。更多内容请见下文。

创建偏好管理 Activity

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

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

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

希望对您有所帮助...

关于android - 我们如何控制 Android 同步适配器首选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5486228/

相关文章:

java - Android Java Handler,线程同步块(synchronized block)(notifyall和Wait)

android - 在 Android 中获取图片的一部分?

android - 如何让一个新启动的 Activity 在 Android TalkBack 中宣布自己?

java - 致命异常 : Main java. lang.RuntimeException:无法实例化 Activity ComponentInfo

android - 判断文件夹是否存在,不存在则创建

java - Android 和 Guice - 注入(inject)通用类型类?

java - 无法使用 map 启动 Activity

android - 将任务移回无法正常工作的android

codeigniter - fatal error : Class 'CI_Model' not found in

function - Magento fatal error 无法重新声明函数主题Blanco