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

发布于 2024-10-29 06:09:54 字数 1307 浏览 6 评论 0原文

在尝试编写自定义 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>

该代码在常规设置中生成了一个条目(帐户设置):

单击帐户设置后,我收到如下错误,并且设备不必要地重新启动。

错误/AndroidRuntime(30057):android.util.AndroidRuntimeException:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

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

  1. 任何人都可以帮助解决这个问题吗?

  2. 我们如何通过代码控制这些类型的首选项?

In an attempt to write a custom Android sync adapter I followed this.
I was success at showing an entry (Account settings) in General setting with the following code snippet from above said example.

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

The code resulted me an entry (Account Settings) in General settings:

Upon clicking the Account Settings I'm getting an error as follows and the device reboots unnecessarily.

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?

I know this error can be solved through code. Since "Account Settings" preference is XML-based code I'm stuck with the error.

  1. Can anyone help to solve the Issue?

  2. How do we control these kind of preferences through code?

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

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

发布评论

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

评论(2

梦里兽 2024-11-05 06:09:54

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

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

设置帐户首选项 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 文档找到这些内容的——如果这个问题的未来读者有这些参考资料,那么如果能将这些链接起来就好了。)

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

创建首选项管理活动

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

从“首选项编辑”意图获取帐户

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

希望有帮助...

I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:

  1. Set up Account Preferences XML
  2. Create an activity to manage preferences
  3. Extract the account information from the "preference editing" Intent

Setting up the account preferences XML

I used an account_preferences.xml very similar to the one in the SDK sample and the c99 Last.fm application. Consider the following snippet:

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

Given this, here are some of the important points I've found: (Note that I've found these through experimentation and not through any specific Android documentation -- if future readers of this question have those references, it'd be great to link those in.)

  • The android:key for this PreferenceScreen must be "account_settings" or Android will not find & display your preferences
  • By using an explicit Intent and specifying the targetPackage and targetClass, android will start your Activity directly and you don't need to worry about an Intent filter.
  • Android stores the Account object for the currently selected account in this Intent's Extras -- which is very important on the receiving end so you can know which account you're managing. More on this below.

Creating the preference managing Activity

Next I created an Activity to correspond to the package and class specified in the above XML. Note that as far as I can tell, the choice of Activity is up to you -- it's most common to subclass android.preference.PreferenceActivity but I've also subclassed Activity directly. Standard Activity development guidelines apply here...

Getting the Account from the "preference editing" Intent

When your Activity starts up, you can extract the corresponding Account object from the Extras Bundle (using this.getIntent().getExtras()) and the key "account". Recall that this Intent will be the one that you specified in preferences XML file initially. (Again, I could not find doc on this so found it by dumping the contents of the Extras Bundle passed in with my Intent.) Once you have the Account, it should be straightforward to load/save preferences for that account using SharedPreferences, your database, or whatever other method you prefer.

Hope that helps...

凝望流年 2024-11-05 06:09:54

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

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

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

The files/resources refered to above in the are not in the stand alone package : this is the only thing the author forgot to adapt i guess : you have to create your own preference class .
here is my class :

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

and here is the preferences file : 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>

you will have to adapt those, or have a deeper look at the files in his last.fm project.

hope this helps, good luck.

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